By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,415 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,415 IT Pros & Developers. It's quick & easy.

SQL Server connection problem

P: 93
Iím trying to learn how to connect to an SQL Server database (Iím using the AdventureWorks sample database), but Iím having problems.

Hereís the code Iím using:
Expand|Select|Wrap|Line Numbers
  1. // define connection string for database server
  2. string connectionString = "server=<server name>; database=AdventureWorks; uid=<user name>; pwd=;";    // no password
  3.  
  4. // define SqlConnection connection to database using connection string
  5. SqlConnection conn = null;
  6. try
  7. {
  8.     conn = new SqlConnection (connectionString);
  9. }
  10. catch (Exception exc)
  11. {
  12.     MessageBox.Show (exc.Message, "conn = new SqlConnection (connectionString);");
  13. }
  14.  
  15. // define SqlCommand command or command string that contains query
  16. string commandString = "SELECT * from <table name>";
  17.  
  18. // define SqlDataAdapter data adapter using command string & connection object:
  19. SqlDataAdapter dataAdapter = new SqlDataAdapter (commandString, conn);
  20.  
  21. // create new DataSet object (create DataSet)
  22. DataSet ds = new DataSet ();
  23.  
  24. try
  25. {   // fill dataset object with query result via data adapter (for SELECT)
  26.     dataAdapter.Fill (ds, "<table name>");
  27. }
  28. catch (Exception exc)
  29. {
  30.     MessageBox.Show (exc.Message, "dataAdapter.Fill (ds, \"<table name>\");");
  31. }
  32.  
  33. // result of query now stored in dataset object in table "<table name>"
  34. // get reference to table by using indexer property of dataset object's Tables collection
  35. dataTable = ds.Tables ["<table name>"];
  36.  
At the statement
Expand|Select|Wrap|Line Numbers
  1. dataAdapter.Fill (ds, "<table name>");
I'm getting the following error: "Login failed for user '<my user name>'."

As near as I can make out, the database AdventureWorks that I see in SQL Server 2008 Management Studio and that I'm trying to access resides at either C:\Program Files\Microsoft SQL Server\100\Tools\Samples\AdventureWorks 2008 OLTP or
C:\Program Files\Microsoft SQL Server\100\Tools\Samples\AdventureWorks OLTP.
I thought that maybe the problem was I was supposed to specify the full path name in the connection string, but that didn't help. Any suggestions?
Mar 16 '09 #1
Share this Question
Share on Google+
1 Reply


ck9663
Expert 2.5K+
P: 2,878
This is more of a question on your front-end app, not SQL server.


-- CK
Mar 16 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.