1

我构建了一个 Windows 窗体应用程序项目并使用 Linq-to-SQL 连接了一个 SQL Server 数据库。一切正常,直到出现此错误:

无法打开用户默认数据库。登录失败。
用户登录失败

连接字符串是:

 Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\ShippingDocumentDB.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True

没有找到任何解决方案。

4

3 回答 3

0

It sounds like the default database for the current user account has been removed, or permissions to that database have been removed for the user you are looking at.

  • Has this user been able to login before?
  • Can you connect to SQL server as a different user and find out what that user's default database is?

As an example, try runnning this against sql server when logged in to SQL Server Management Studio (works on SQL Server 2005 onwards):

SELECT name, default_database_name
FROM sys.server_principals

Check the value in the second column for the user in question, and then see if that database exists and the user has permission to it.

EDIT. We now know from your connection string that you are using a user instance. YOu need to check that the database file is available. It may be attached to SQL Server's main instance, for example, which would mean the file is in use and can't be attached to the user instance. Try making a copy of the database file using windows explorer and put the name of that file copy into the AttachDbFilename parameter - that may show that this is the case.

于 2013-05-12T19:33:21.727 回答
0

也许您应该为您的特定数据库定义此用户;

于 2014-10-27T14:03:08.133 回答
-1

对于 SQL Server 连接字符串,它应该采用以下格式:

 SERVER=[SVRNAME\INSTANCENAME];USER=[USERID];PWD=[PASSWORD];DATABASE=[DATABASENAME]
于 2013-05-12T19:46:30.110 回答