Search code examples
c#sqlconnection-stringlocaldbmdf

How to make a published app run on other computers using a LocalDB (.MDF)?


I'm having trouble making an application developed to run on other computers. This application uses a local database ( .mdf ) that is in the same project directory.

When I publish the application and install on another computer , it installs correctly but when I run appears an error related to the connection to the database.

Below is the code of the ConnectionStrings in 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>

And here's the ConnectionString to use in my class that connects to the database.

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

This is the error message I get when I run the application on another computer:

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

Is there any way to make the application recognize the .mdf file on any computer ?


Solution

  • you need to make your database visible from the remote computer, all you need to do is change the connection string to

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

    you also need to be sure that the user running the app have a valid login in your local SQL otherwise you'll need to change Integrated Security=True to

    User ID=user;Password=passwd