Sunday, February 26, 2012

Error : Cannot open user default database. Login failed.

I work with Microsoft visual studio 2005.
i get data from a table by ADO.NET :

SqlConnection sqlConn = new SqlConnection(strConnectionString);
sqlConn.Open();
.....

it works fine.

but when i open the SQL Server 2005 express edition and connect to the server (inside the same computer) to check something and then get back to the VS and run the code again ,the Open() function fails with the exception :

{"Cannot open user default database. Login failed.\r\nLogin failed for user 'DOMAIN\\asaf_a'."


disconnecting the server doesn't help and even closing the SQL Server 2005 application doesn't help.
Only restarting the computer enables the Open() command to work again.

Why ?

Every user has a default database specified. If you are using a SQLConnectinstring you are normally redirecting the user within the connectionstring to a specific database (Initial Catalog). By connecting with another tool which does not have the availbility to choose a starting database the user is redirected to its "home" / default database. If the database was dropped in the meantime, or the user was denied access / or even not granted, the login procedure will fail. You can set the user defaults database with using the procedure sp_defaultdb or by using any gui which can administer the logins.

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de|||

Thanks jens,

i solved the problem.

It was my connection string all along. it was wrong. This was the problem.

I changed the connection string from :

string strConnectionString = @."Server=.\SQLTESTING;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL.3\MSSQL\Data\TestDB.mdf;Integrated Security=SSPI";

To :

string strConnectionString = @."Server=.\SQLTESTING;Integrated Security=TRUE;Database=TestDB";

now i can use (Open) the TestDB Database from the VS code even when the SQL Server 2005 is also connected to this database.

Thanks!

No comments:

Post a Comment