C# 无法从文件连接到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16877770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't connect to database from file
提问by Kmaczek
I try to connect through: Microsoft SQL Server Database File (SqlClient), but I recieve error:
我尝试通过以下方式连接:Microsoft SQL Server 数据库文件 (SqlClient),但收到错误消息:
The attempt to attach to the database failed with the following information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
尝试附加到数据库失败,并显示以下信息:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:SQL 网络接口,错误:52 - 无法找到本地数据库运行时安装。验证 SQL Server Express 是否已正确安装并且本地数据库运行时功能已启用。)
So, i click Connect to Database in Server Explorer
因此,我在服务器资源管理器中单击连接到数据库


This window show up. In which i choose: Microsoft SQL Server Database File (SqlClient) and then browse my .mdf database file.
出现这个窗口。我在其中选择:Microsoft SQL Server 数据库文件 (SqlClient),然后浏览我的 .mdf 数据库文件。


Clicking ok gives error mentioned before:
单击确定会出现之前提到的错误:


Server is running, I use windows authentication in database. Any sugestions?
服务器正在运行,我在数据库中使用 Windows 身份验证。有什么建议吗?
采纳答案by lachs
回答by Ajay
Try to use SQL Server Authentication. Make sure that your SQL serviceis running or not. I think this will help you.
尝试使用SQL Server 身份验证。确保您的SQL 服务正在运行。我认为这会对你有所帮助。
回答by user3477428
I had faced same issue when I deployed my software on test machine.
Solution:
当我在测试机器上部署我的软件时,我遇到了同样的问题。
解决方案:
- Check the SQL server version using which the .mdf was build.
If you don't know execute command
SELECT @@VERSION. - Install correct version of SqlLocalDB.msi from following link.
http://www.microsoft.com/en-us/download/details.aspx?id=29062http://www.microsoft.com/en-my/download/details.aspx?id=42299.
- 检查用于构建 .mdf 的 SQL 服务器版本。如果你不知道执行命令
SELECT @@VERSION。 - 从以下链接安装正确版本的 SqlLocalDB.msi。
http://www.microsoft.com/en-us/download/details.aspx?id=29062 http://www.microsoft.com/en-my/download/details.aspx?id=42299。

