469,346 Members | 6,542 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,346 developers. It's quick & easy.

SQL Server connection problem

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
1 1806
ck9663
2,878 Expert 2GB
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.

Similar topics

13 posts views Thread by Fortepianissimo | last post: by
reply views Thread by Suresh | last post: by
reply views Thread by Kris Mattheus | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.