从C#数据库应用程序加载数据时出错

问题描述:

我创建了一个C#数据库应用程序,数据库文件的类型为.mdf。该应用在我的PC和其他已安装MS SQL Server的PC上运行良好。在没有安装SQL Server的其他机器上,安装过程显示未处理的异常,此后,应用程序打开,但没有加载数据。从C#数据库应用程序加载数据时出错

异常说:

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.).

这是详细完整例外:

*System.Data.SqlClient.SqlException (0x80131904): System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at ...

... at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ClientConnectionId:00000000-0000-0000-0000-000000000000 Error Number:2,State:0,Class:20*

应用程序可以成功运行,但没有从数据库中的数据被加载。 我是否必须在所有机器上安装SQL Server express,以便应用程序可以加载其所需的数据,或者只是SQL Server的某些组件就足够了。其他PC禁用Windows更新。

对于这里更多信息,应用程序文件:

  • Data177k.mdf.deploy
  • Data177k_log.ldf.deploy
  • deatach.application
  • deatach.exe.config.deploy
  • deatach.exe.deploy
  • deatach.exe.manifest
  • setup.ex e

我该怎么做才能让应用程序加载它的数据库,即使在没有SQL服务器的PC上安装。

+2

您在哪个版本的SQL Server中创建了数据库? 您通常需要在PC上安装SQL Server,以便它能够附加和使用.MDF文件。 – Culme

+0

SQL Server 2014 express @Culme – admiri92

+0

您的应用程序可以在每个数据库请求失败时成功运行?我认为你的错误处理有一些主要问题......在任何情况下,错误消息似乎都非常清楚:“确认SQL Server Express已正确安装。”为什么你会期望这个工作*没有* SQL服务器? – Luaan

我建议您阅读SQL Server LocalDB,它允许您在客户端计算机上使用.MDF文件发布必要的配置文件,而无需在其上安装SQL Server/Express。

MSDN SQL 2014 Download and information page