c# 连接到本地 MDF DB 文件
在我的开发计算机上,我有 MS SQL Server/Visual Studio 2005。我的程序可以正确连接到我的本地数据库并使用它。但是我的另一台计算机(非开发)没有 MS SQL Server/Visual Studio 2005,并且无法连接到数据库。它输出以下内容:
“建立与服务器的连接时发生错误。连接到 SQL Server 2005 时,此故障可能是由于在默认设置下 SQL Server 不允许远程连接而导致的。... “(错误:26)。
这是否意味着我必须在非开发计算机上安装 SQL Server 2005?还有其他办法吗?
我的连接字符串是:
“Data Source=.\SQLEXPRESS;AttachDbFilename=\”” + 目录.GetCurrentDirectory() + "\DB.mdf\";集成安全性=True;用户实例=True";
On my development computer I have MS SQL Server/Visual Studio 2005. My program can correctly connect to my local DB and use it. However my other computer (non-dev) does not have MS SQL Server/Visual Studio 2005 and does not connect to the DB. It spits out the following:
"An error has occurrred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. ..." (Error: 26).
Does this mean I have to install SQL Server 2005 on my non-dev computers? Is there any other way?
My connection string is:
"Data Source=.\SQLEXPRESS;AttachDbFilename=\""
+ Directory.GetCurrentDirectory()
+ "\DB.mdf\";Integrated Security=True;User Instance=True";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的连接字符串告诉 Sql Server Native Client ADO.NET 提供程序尝试连接到名为 SQLEXPRESS 的 Sql Server 实例,该实例将管理存储在文件 DB.mdf 中的数据库>。由于您的客户端计算机没有安装 Sql Server Express,因此它不会找到要连接的数据库。
您将需要:
Your connection string is telling the Sql Server Native Client ADO.NET Provider to attempt to connect to a Sql Server instance named SQLEXPRESS that will manage the database stored in a file DB.mdf. Since your client computer does not have Sql Server Express installed, it's not going to find a database to connect to.
You will need to:
如果您想在其他电脑/服务器上运行它,您还需要安装 SQL Server(需要将 .mdf 附加到它)或 SQL Server Express。
另一个技巧是更改连接字符串,以便它指向运行数据库的另一台电脑/服务器。
If you want to run it on that other pc/server you also need to have SQL Server (need to attach your .mdf to it) or SQL Server Express installed.
Another trick would be to change your connection string so that it points out to another pc/server where your database runs on.
是的,您确实需要目标计算机上可用的 MS-SQL (Express)。本地安装或连接到服务器。
将 SQl Express 包含在 Setup.exe 中并不困难(请参阅先决条件)。
另一种方法是使用 SQL-CE 或 Sqlite 或(甚至)MS-Access。它们是“嵌入式”数据库引擎,因此您只需分发 DLL。
Yes, you do need MS-SQL (Express) available on your target computers. Either a local install or a connection to a server.
It is not that difficult to include SQl Express in a Setup.exe (see PreRequisites).
An alternative is to use SQL-CE or Sqlite or (even) MS-Access. They are 'embedded' database engines so that you only need to distribute DLLs.