0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anonymous
  • 1,978
  • 2
  • 28
  • 37
  • Can you open the connection if you set `IntialCatalog = "master";` ? – driis Dec 15 '12 at 15:59
  • yes. It will connect if I use master. – Anonymous Dec 15 '12 at 16:03
  • In that case, your _login_ works, so you either a) have no access to the "Northwind" database, or b) the "Northwind" database does not exist. Doublecheck if you have a typo somewhere. – driis Dec 15 '12 at 16:05
  • Doesn't appear to be a typo anywhere. If I right click on the Northwind DB in management studio, go to properties, go to permissions, I'm listed as one of the users. Also, if I click on the security folder, logins, then go into the properties for my account, I'm mapped to the Northwind DB as db_owner. Is there another place I'm missing? – Anonymous Dec 15 '12 at 16:08
  • Sounds like you have everything correctly setup ... Is the username in the error message your own user name ? What is the output if you run this in management studio on master: `select name from syscatalogs;` ? – driis Dec 15 '12 at 16:11
  • I think I know the problem...Northwind is showing in the object explorer, but not as an available database. – Anonymous Dec 15 '12 at 16:17
  • If I change the code to read: builder.DataSource = "."; instead of builder.DataSource = ".\\SQLEXPRESS"; if works just fine...I must have something configured horribly. Any ideas what it could be? – Anonymous Dec 15 '12 at 16:20
  • Sounds like you have two instances of SQL server, one installed as the default instance (.) and a sql express instance at .\\SQLEXPRESS. See if you can connect to both in SSMS. – driis Dec 15 '12 at 16:31

3 Answers3

1
  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. :)

spajce
  • 7,044
  • 5
  • 29
  • 44
1

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;";
George Papatheodorou
  • 1,539
  • 19
  • 23
0

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");
}
steoleary
  • 8,968
  • 2
  • 33
  • 47