创建安装包后,程序看不到数据库

时间:2012-09-22 21:55:18

标签: c# visual-studio-2010 database-connection mdf

我在c#中开发了windows表单应用程序。我已经习惯了这个Microsoft Visual Studio 2010.现在我想生成安装包(或允许我将程序提供给人们的东西),但我遇到了一些奇怪的问题。

当我使用调试模式时 - 一切正常,所以我从MyDocuments \ Visual 2010 \解决方案名称\ Project Name \ bin \ Debug中获取编译程序并将其交给我的妻子进行测试...但是在她的计算机程序没有运行并出现此错误:

System.Data.SqlClient.SqlException: An error has occurred with the onset of the network or connect to a SQL Server server. Can not find server or it is not available. Verify that the instance name is correct and that the configuration of SQL Server allow remote connections. (Provider: SQL Network Interfaces, error: 26 - Error locating a specific server / instance)
   in System.Data.ProviderBase.DbConnectionPool.GetConnection (DbConnection owningObject)
   in System.Data.ProviderBase.DbConnectionFactory.GetConnection (DbConnection owningConnection)
   in System.Data.ProviderBase.DbConnectionClosed.OpenConnection (DbConnection outerConnection, DbConnectionFactory connectionFactory)
   in System.Data.SqlClient.SqlConnection.Open ()
   in System.Data.Common.DbDataAdapter.QuietOpen (IDbConnection connection, ConnectionState & originalState)
   in System.Data.Common.DbDataAdapter.FillInternal (DataSet dataset, DataTable [] DataTables, Int32 startRecord, Int32 MaxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill (DataTable [] DataTables, Int32 startRecord, Int32 MaxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill (DataTable dataTable)
   in Home_Video_Catalog.HVCDBDataSetFilmsTableAdapters.FilmsTableAdapter.Fill (FilmsDataTable dataTable) in C: \ Documents and Settings \ Aru \ My Documents \ Visual Studio 2010 \ Projects \ Home Video Catalog \ Home Video Catalog \ HVCDBDataSetFilms.Designer.cs: line 868
   in Home_Video_Catalog.MainWindow.MainWindow_Load (Object sender, EventArgs e) in C: \ Documents and Settings \ Aru \ My Documents \ Visual Studio 2010 \ Projects \ Home Video Catalog \ Home Video Catalog \ MainWindow.cs: line 27
   in System.Windows.Forms.Form.OnLoad (EventArgs e)
   in DevComponents.DotNetBar.Office2007RibbonForm.OnLoad (EventArgs e)
   in System.Windows.Forms.Form.OnCreateControl ()
   at System.Windows.Forms.Control.CreateControl (Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl ()
   in System.Windows.Forms.Control.WmShowWindow (Message & m)
   at System.Windows.Forms.Control.WndProc (Message & m)
   in System.Windows.Forms.ScrollableControl.WndProc (Message & m)
   in System.Windows.Forms.ContainerControl.WndProc (Message & m)
   in System.Windows.Forms.Form.WmShowWindow (Message & m)
   in System.Windows.Forms.Form.WndProc (Message & m)
   in DevComponents.DotNetBar.Office2007RibbonForm.WndProc (Message & m)
   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)

...所以我想如果我可以创建安装程序项目并为我的项目创建设置就可以了......但错误与之前的错误代码相同。

我不太明白,因为我的项目中的配置文件(项目中的app.config和Properties \ settings.settings)是正确的连接字符串:

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HVCDB.mdf;Integrated Security=True;User Instance=True"

同样有趣的是错误消息中的路径在我的计算机上有效(这是我投影的地方)

请帮帮我:)。

1 个答案:

答案 0 :(得分:0)

连接字符串中的语法Data Source =。\ SQLEXPRESS指的是本地计算机上安装的SqlServer Express实例。
从错误消息我想,目标安装计算机上没有安装SqlServer Express 所以你有两个选择:

  • 在目标安装机器上安装Sql Server Express
  • 更改连接字符串以引用您的工作机器并使用 在那里找到了数据库目录。

    Data Source=YOURMACHINENAME\SQLEXPRESS;Initial Catalog=YOURDATABASENAME;.....
    

当然,从另一台机器连接到您的开发机器需要打开防火墙端口,配置Sql Server进行远程连接,配置用户安全性......

Remote connections
Firewall

如果您不想在运行应用程序的每台计算机上转到安装SQL Server的路径,那么您可以调查其他数据库,如SQLiteSql Compact Edition或非共享版本的SqlServer叫LocalDB