Go Back   Computer Forums > General Computing > Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 03-15-2009, 09:50 PM   #1
Baseband Member
Join Date: Nov 2008
Posts: 91
Default SQL Server connection problem

I知 trying to learn how to connect to an SQL Server database (I知 using the AdventureWorks sample database), but I知 having problems.

Here痴 the code I知 using:
// define connection string for database server
string connectionString = "server=<server name>; database=AdventureWorks; uid=<user name>; pwd=;";    // no password

// define SqlConnection connection to database using connection string
SqlConnection conn = null;
    conn = new SqlConnection (connectionString);
catch (Exception exc)
    MessageBox.Show (exc.Message, "conn = new SqlConnection (connectionString);");

// define SqlCommand command or command string that contains query
string commandString = "SELECT * from <table name>";

// define SqlDataAdapter data adapter using command string & connection object:
SqlDataAdapter dataAdapter = new SqlDataAdapter (commandString, conn);

// create new DataSet object (create DataSet)
DataSet ds = new DataSet ();

{   // fill dataset object with query result via data adapter (for SELECT)
    dataAdapter.Fill (ds, "<table name>");
catch (Exception exc)
    MessageBox.Show (exc.Message, "dataAdapter.Fill (ds, \"<table name>\");");

// result of query now stored in dataset object in table "<table name>"
// get reference to table by using indexer property of dataset object's Tables collection
dataTable = ds.Tables ["<table name>"];
At the statement
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?

BobLewiston is offline   Reply With Quote
Old 03-16-2009, 06:21 AM   #2
Site Team
root's Avatar
Join Date: Mar 2004
Posts: 7,872
Default Re: SQL Server connection problem

are you sure that there is no password for the user?

try creating a password for the userm (you do this in the security/users section of SQL server).

then try having the user log in.

Also I think that you have to se a SQL server login when accessing in this way, even if you have mixed mode security activated I don't think that the windows logon can be used to make the connection, (in case the user is a windows user).

I didn稚 fight my way to the top of the food chain to be a vegetarian
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 03-16-2009, 07:35 AM   #3
In Runtime
Daeva's Avatar
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: SQL Server connection problem

You should be able to log-in with a windows account. Perhaps it's a beast of a different color, but Web Applications in Visual Studio connection to a SQL Server database login as the Network Service account by default. Although I did need to give that windows login permissions on the database before it would work.
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
Daeva is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 06:37 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0