SQL Server connection problem

BobLewiston

In Runtime
Messages
182
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:
Code:
// 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;
try
{
    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 ();

try
{   // 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
Code:
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?
 
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).
 
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.
 
Back
Top Bottom