0

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 ?

Diego Lima
  • 27
  • 5

2 Answers2

1

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

jorge.armando
  • 50
  • 1
  • 7
  • In fact, the application users will not use the same bank. In each software installation, will be a new database ( .mdf ) . I wanted this .mdf file accompany the installation and independent of the computer package, the app would recognize the database . – Diego Lima Jul 01 '15 at 13:02
  • there is no problem with the code, the problem is on the access to the database, just try attaching the database on the local sql server where the app is installed and change the connectionstring to Data Source=localhost;Initial Catalog=bd_Cadastro.mdf;Integrated Security=True – jorge.armando Jul 01 '15 at 13:19
  • Sorry, but this didn't worked. My app, is just to save some names in a datagridview and save these datas on a .MDF File. The idea of ​​the app is to make one to lead this software to various places and keep this data safe. It is a user who does not know what is SQL Server or anything . So I want to make an application where it need only install and use. – Diego Lima Jul 01 '15 at 13:41
0

This is what inside 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(); }
            }
Diego Lima
  • 27
  • 5