Connection failed:
SQLState: '42000'
SQL Server Error: 4064
[Microsoft][ODBC SQL Server Drive][SQL Server]
Cannot open user default database. Login failed.
OR
Error 21002: [SQL-DMO]User 'database account'
already exists.
This happens when a database has been restored onto a different MSSQL Server, the
database user account no longer refers to the
SQL Server login (as the SPID - Service Profile Identifier, is no longer consistent) and you can not edit the permissions of the SQL Server Login as you get an error message saying that user already exists.
Solution
Login as SA (into Query Analyzer) and run the SP on the database in question:
sp_change_users_login 'Update_One',
'[Database Account Name]',
'[SQL Server Login Name]'
Related (and interesting)
--changes default database
sp_defaultdb '[SQL Server Login Name]',
'[Database Name]'
--list users (run on actual database)
sp_change_users_login 'Report'
References
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_ca-cz_8qzy.asp
http://www.mcse.ms/archive81-2003-12-190563.html