【问题标题】:How to make a published app run on other computers using a LocalDB (.MDF)?如何使用 LocalDB (.MDF) 使已发布的应用程序在其他计算机上运行?
【发布时间】:2015-07-01 12:45:15
【问题描述】:

我在开发可在其他计算机上运行的应用程序时遇到问题。此应用程序使用位于同一项目目录中的本地数据库( .mdf )

当我发布应用程序并在另一台计算机上安装时,它可以正确安装,但是当我运行时出现与数据库连接相关的错误。

以下是app.configConnectionStrings的代码。

    <?xml version="1.0"?>
<configuration>
  <system.windows.forms jitDebugging="true" />
  <configSections>
  </configSections>
  <connectionStrings>
    <clear />
    <add name="ConexaoBD" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\bd_Cadastro.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

这里是ConnectionString 在我的类中使用,它连接到数据库。

        private string str_conexao = ConfigurationManager.ConnectionStrings["ConexaoBD"].ConnectionString;

这是我在另一台计算机上运行应用程序时收到的错误消息:

************** Exception Text **************
System.ArgumentException: Invalid value for key 'attachdbfilename'.
   at EVO_Next_List.cls_Conexao.ExecutarDataSet(String str_sql)
   at EVO_Next_List.frmPrincipal.CarregaRegistros()
   at EVO_Next_List.frmPrincipal.frmPrincipal_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at MetroFramework.Forms.MetroForm.OnLoad(EventArgs e)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at MetroFramework.Forms.MetroForm.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

有什么方法可以让应用程序识别任何计算机上的 .mdf 文件?

【问题讨论】:

  • 显示EVO_Next_List.cls_Conexao.ExecutarDataSet()里面的内容。
  • bd_Cadastro.mdf 数据库文件复制到哪里?您确定 |DataDirectory| 快捷方式映射到此位置吗?
  • 你最后是怎么解决这个问题的?

标签: c# sql connection-string localdb mdf


【解决方案1】:

您需要使您的数据库在远程计算机上可见,您只需将 连接字符串 更改为

Data Source=server;Initial Catalog=bd_Cadastro.mdf;Integrated Security=True

您还需要确保运行该应用的用户在您的本地 SQL 中具有有效登录名,否则您需要将 Integrated Security=True 更改为

用户 ID=user;密码=passwd

【讨论】:

  • 其实应用用户不会使用同一个银行。在每次软件安装中,都会有一个新的数据库(.mdf)。我希望这个 .mdf 文件伴随安装并且独立于计算机包,应用程序将识别数据库。
  • 代码没有问题,问题出在对数据库的访问上,只需尝试在安装应用程序的本地sql服务器上附加数据库并将连接字符串更改为Data Source=localhost ;初始目录=bd_Cadastro.mdf;集成安全=True
  • 抱歉,这不起作用。我的应用程序只是在 datagridview 中保存一些名称并将这些数据保存在 .MDF 文件中。该应用程序的想法是制作一个将这个软件带到各个地方并保持这些数据安全的人。这是一个不知道什么是 SQL Server 或任何东西的用户。所以我想制作一个只需要安装和使用的应用程序。
【解决方案2】:

这是EVO_Next_List.cls_Conexao.ExecutarDataSet()里面的内容

 public DataSet ExecutarDataSet(string str_sql)
            {
                SqlConnection Conn = new SqlConnection(); // Faz Conexão;
                SqlCommand cmdComando = new SqlCommand(); // Recebe o comando.
                SqlDataAdapter DataAdt = new SqlDataAdapter(); // Preenche o DataTable.
                DataSet DS = new DataSet();

                try
                {
                    Conn = AbrirBanco();
                    cmdComando.CommandText = str_sql;
                    cmdComando.CommandType = CommandType.Text;
                    cmdComando.Connection = Conn;

                    DataAdt.SelectCommand = cmdComando;
                    DataAdt.Fill(DS); // Preenche a Datatable

                    return (DS);
                }

                catch (Exception Erro)
                { throw Erro; }

                finally
                { Conn.Close(); }
            }

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多