I have received following question nearly 10 times in last week though emails. Many users have received following error while connecting to the database. This error happens when database is dropped for which is default for some of the database user. When user try to login and their default database is dropped following error shows up.
Cannot open user default database. Login failed.
Login failed for user ‘UserName’. (Microsoft SQL Server, Error: 4064)

The fix for this problem is very simple.
Fix/Workaround/Solution:
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.

Now click on connect and it will successfully log you in. Once you login in the system run following statement for your username and it should fix your problem. Make sure to replace [test] with your username and master with yourdatabase name.
ALTER LOGINÂ [test] WITH DEFAULT_DATABASE = master
GO

Watch SQL in Sixty Seconds video to Resolve Connection Error:
[youtube=http://www.youtube.com/watch?v=1k00z82u4OI]
Reference : Pinal Dave (http://www.SQLAuthority.com)






282 Comments. Leave new
Thank you !
Thanks a lot…saved my a**
I can see this is not new but this articles saved my critical time today. I was doing file group maintenance on a server and brought the database online. I did not know that the database for default for that server. I could not bring it online. I searched google and get to your article. I reconnected to server in SSMS using option, I selected Master and I was in and was able to bring my database online. Thanks once again. Keep up the good work.
thanks for that , it really worked well !
Thanks, worked for me !
mohit,
hi, i did the same as u told but unable to take backup offline
solved my problem.. it work fine
lot of thank Pinal
nice post
Thanks again
Abdul Jabbar Patel
worked like a charm. quick and easy.
thanks pinal
Thanks a lot.
It’s really a great post.
Sunil Sharma
Thanks!
Hmm,
I have the same problem but I cannot fix it with adding the “testdb” name in the “Connect to database” field.
If I fill in “master” it works. But why dows it not work with “testdb” which is assigned to my “testuser”?
thanks very very much, I was going crazy…
thanks, very helpful!
Thanks, this help me to solve the problem.
When i try to run query for my domain id
ALTER LOGIN [domain\user] WITH DEFAULT_DATABASE=database
Its give me an error incorrect sentence near login in sql server 2008
It worked great sir, thnx for the solution
THANKS A LOT ..IT WORKED!!:D
Alter Login not work! Why?
I have to activate some command?
Thanks.
My sql server managment studio is 2005 but alter login not work.
I enter on the database master but the command alter login not work.
Thanks
I encountered with the same problem when I change the domain/ change the password of my win login.
To resolve this, I log-in through the SQL Authentication and remove my windows login from the SQLServer\Security\Logins. After remove, I added my windows login and it’s resolve the issue.
This post saved my day today ! Thanks a lot.