“Cannot open user default database. Login failed.” after installing SQL Server Management Studio Express

限于喜欢 提交于 2019-11-27 05:17:44

First, try to isolate your problem:

  1. Take a backup of the file! Some of the steps below can, apparently, in some circumstances cause the file to vanish.
  2. Are you sure you are connecting to the same instance through Management Studio as the program is?
  3. If possible, try to shut down the instance that you are not expecting to use.
  4. Set the user's default database to master and try to make the program logon.
  5. Try to login as the user through Management Studio - since you have integrated security, you should open Management Studio as the program's user.
  6. Are you using "User instances" - perhaps without knowing it? If so, this may be helpful: http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx

I haven't worked much with files being attached in the way your program does - but you write that you attached the DB in the Management Studio as well. Have you tried detaching it there before running your program? Perhaps you are seeing the Management Studio and your program competing for exclusive access to the MDF-file?

EDIT: I added point 6 above - this is new in my own list of TODOs when troubleshooting this type of Login failed. But it does sound a lot like what you're experiencing.

EDIT2: In the first edit, new item was added to the list. So the numbers in the comments doesn't correspond with the numbers in the answer.

This may not be answering your question specifically, but it will help other with similar issue

The problem is your user is defaulted to a database which is not accessible for any reason (can be renamed, removed, corrupted or ...) To solve the issue just follow the following instruction

  1. Try to login again on the login page there is other tabs select "Connection Properties".
  2. under the tab locate "Connect to database" and select an existing database you have access to like tempdb or master
  3. Once you are connected to the SQL Server Instance execute the below TSQL to assign the login a new default database.

    Use master
    GO
    
    ALTER LOGIN [yourloginname] WITH DEFAULT_DATABASE = TempDB
    GO
    

Alternatively once you connected change your default database name to master via UI

Article taken from : http://www.mytechmantra.com/LearnSQLServer/Fix-cannot-open-user-default-database-Login-failed-Login-failed-for-user-SQL-Server-Error/

I finally figured this out, and my situation is different than every other I've read about tonight.

I had restored my database from a backup. I knew that there was a particular login user that I had been using, so I created that user in SSMS. However, there was already a user by that name under the database that had come in with the backup.

Since I had screwed around so much trying to fix this, I wasn't able to delete the user under the DB easily. I deleted the database and restored again. Then:

  1. Delete the user under the Databases->[my database]->Users
  2. Create the user again in Security->Logins (not under your DB, although that probably works too.
  3. Go to the newly created user. Select properties. Then under User Mappings, tell it to make your database the default. Give it read and write access.

Summary: I had two users. One that came with the DB, and one that I had created. Remove the one that came with the DB and create your own.

This problem manifested for me when I took my default db offline. Next thing I know I couldn't login. Switching to the Connection Properties tab and selecting the drop down to change the database I want to connect to also failed.

It let me in right away once I manually typed master as the db I wanted to connect to (on the Connection Properties tab).

First click on Option>> Button of “Connect to Server” Prompt.

Now change the connect to database to any existing database on your server like master or msdb.

More Details

https://blog.sqlauthority.com/2008/11/04/sql-server-fix-error-4064-cannot-open-user-default-database-login-failed-login-failed-for-user/

I've also had this same problem, it turned out that I was trying to access the built in membership classes (in a view), and that .Net was trying to create the database in the App_Data folder:

@Membership.GetUser().ProviderUserKey

This will trigger the system to try and create a database based in the built in membership system, which may not be the way your system is setup.

I had a similar problem had to simply download SQL Express Utility that is capable of starting User Instances. SSEUtil is a tool written by the Visual Studio team to help troubleshoot User Instance issues, you can read more about it in the read me file that is installed with the utility. http://www.microsoft.com/downloads/details.aspx?FamilyID=fa87e828-173f-472e-a85c-27ed01cf6b02&DisplayLang=en.

Hope this will help.

wookee80

In my case I had to set "connect to any database" right path:

On your instance, go to Security , then to Logins.

Right Click on there, you will see properties and you should click on Securables.

There it give possibility to connect to any database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!