c# 连接到本地 MDF DB 文件

发布于 2024-09-16 14:24:46 字数 461 浏览 3 评论 0原文

在我的开发计算机上,我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

衣神在巴黎 2024-09-23 14:24:46

您的连接字符串告诉 Sql Server Native Client ADO.NET 提供程序尝试连接到名为 SQLEXPRESS 的 Sql Server 实例,该实例将管理存储在文件 DB.mdf 中的数据库>。由于您的客户端计算机没有安装 Sql Server Express,因此它不会找到要连接的数据库。

您将需要:

  • 在客户端计算机上安装 Sql Server 并在那里部署数据库。
  • 切换到 Sql Server Compact Edition(SqlCE - 嵌入式数据库)并重新构建应用程序以使用可移植数据库文件(带有 SqlCE)。
  • 放弃使用强大的数据库引擎并完全切换到 ADO.NET DataSet,将 DataSet 的内容保存/加载到 Xml 文件(通过 WriteXml()ReadXml() )。如果您正在处理的数据量的大小相当有限,那么数据集是维护完整性(通过强类型和定义良好的模式)和可移植性的好方法。

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:

  • Install Sql Server on the client computer and deploy your database there.
  • Switch to Sql Server Compact Edition (SqlCE - embedded database) and re-architect your application to use the portable database file (with SqlCE) instead.
  • Ditch using a robust database engine and switch entirely to ADO.NET DataSets, saving/loading the contents of the DataSet to an Xml file (via WriteXml() and ReadXml()). If the amount of data you are processing is fairly limited in size, DataSets are a good approach to maintaining integrity (via a strongly typed and well-defined schema) and portability.
尹雨沫 2024-09-23 14:24:46

如果您想在其他电脑/服务器上运行它,您还需要安装 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.

孤独难免 2024-09-23 14:24:46

是的,您确实需要目标计算机上可用的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文