SQL does not keep new recordings

1

I'm using a Service-based Database (.MDF) to save some data from an application I'm developing. However, after I close the application, the data that was entered in the table disappears. Here is the code I'm using:

System.Data.SqlClient.SqlConnection sqlConnection1 =
        new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;Integrated Security=True;AttachDbFilename=|datadirectory|\Database2.mdf");
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        sqlConnection1.Open();
        cmd.CommandText = @"INSERT INTO sistemas (nome, potnom, numcolun, colunpac, colunpcc, colunirrad, tempomed) VALUES ('"+ nome+ "','"+ potNom+"','"+ numcolun +"','"+ colunpac+"','" + colunpcc + "','" +colunirrad+"','" + tempoMed + "')";
        cmd.Connection = sqlConnection1;
        cmd.ExecuteNonQuery();
sqlConnection1.Close();

The INSERT is executed correctly, I can query the database if I do not close the application, however, when I restart the application, the table returns empty.

    
asked by anonymous 23.11.2014 / 21:47

1 answer

1

For the information to remain in the .mdf database, you must make a change to the file's properties.

  • Right click on the database found in the Solution Explorer tab
  • Go to properties;
  • In the Copy to Output Directory option, select Copy if newer
  • I hope I have helped.

        
    24.11.2014 / 14:18