Question

Not sure why this is failing...I'm sure it's my fault. Any help would be greatly appreciated.

I'm getting the classic

Cannot open database "Northwind" requested by the login. The login failed. Login failed for user 'MyMachine\MyUserName'.

I can login just fine using windows authentication through SQL Server Management Studio.

I checked in SQL Server Management Studio to make sure that my user has permission to use the Northwind database. I also tried most of the other responses to this question posted here on stackoverflow.

This is my code:

        SqlConnection dataConnection = new SqlConnection();
        try
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = ".\\SQLExpress";
            builder.InitialCatalog = "Northwind";
            builder.IntegratedSecurity = true;
            dataConnection.ConnectionString = builder.ConnectionString;
            dataConnection.Open();

... .

I'm using SQL Server 2008 Express

Was it helpful?

Solution 3

Try doing it this way, it's always worked for me, simpler too as you can create a new SQL connection object directly from a connection string by just specifying the string as a parameter, like this:

string connectionString = @"Server=server\instance;Database=Northwind;Integrated Security=True";
SqlConnection dataConnection = new SqlConnection(connectionString);
try
{
dataConnection.Open();
}
catch (sqlexception e)
{
Messagebox.Show("Error");
}

OTHER TIPS

  1. In your MS SQL Studio right Click the Server and go to properties Security and select SQL Server and Windows Authencation mode

enter image description here

then restart your server.

  1. in your server. go to Security folder and create a new Login enter image description here

  2. enter the Username and Password. just uncheck Enforced Security (for testing purpose only) enter image description here

  3. go to User Mapping and check the database(NorthWind) you want to handle under the new Login account then db_accessadminenter image description here

  4. Click OK

and try your code

SqlConnection dataConnection = new SqlConnection();
            try
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = "UNKNOWN01-PC\\SQLEXRESS2008R2";
                builder.InitialCatalog = "Northwind";
                //builder.IntegratedSecurity = true;
                builder.UserID = "testlogin";
                builder.Password = "1234";
                dataConnection.ConnectionString = builder.ConnectionString;
                dataConnection.Open();
            }
            catch  (Exception)
            {
                throw;
            }

i suspect that the real issue here is the SqlConnectionStringBuilder but i can't explain. im just a beginner. :)

I m just writing this so that I dont fall into the same trap again and again ... The default name of the database is "NORTHWND" and not "NORTHWIND" The name is auto created by windows sql server while importing the .bak file from the oficial site. So this is ok

static string connectionString = "data source=GMDESK028\\SQLSERVER2;initial catalog=NORTHWND;Integrated Security=SSPI;";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top