我无法将应用程序开发为在其他计算机上运行。此应用程序使用位于同一项目目录中的本地数据库(。mdf)。
当我发布应用程序并在另一台计算机上安装时,它会正确安装,但是当我运行时会出现与数据库连接相关的错误。
以下是ConnectionStrings
中app.config
的代码。
<?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文件?
答案 0 :(得分:1)
您需要从远程计算机上看到您的数据库,您只需将连接字符串更改为
Data Source = server; Initial Catalog = bd_Cadastro.mdf; Integrated Security = True
您还需要确保运行该应用的用户在本地SQL中具有有效登录信息,否则您需要将 Integrated Security = True 更改为
用户ID = user;密码= passwd
答案 1 :(得分:0)
这就是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(); }
}