This code adds an entry to the database, this is displayed in the 'DataGridView'. But, if you open the database itself, the added rows do not appear in it. After a while or the restart of the studio, the added rows are no longer displayed in the 'DataGridView'. How to write the lines in the database itself, so that they do not disappear?

SqlConnection connection; string connectionString = ConfigurationManager.ConnectionStrings["DataBases2.Properties.Settings.Database1ConnectionString"].ConnectionString; string query = "INSERT INTO Users VALUES (@userName, @userAge)"; using (connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(query, connection)) { connection.Open(); command.Parameters.AddWithValue("@userName", tbName.Text); command.Parameters.AddWithValue("@userAge", tbAge.Text); command.ExecuteNonQuery(); connection.Close(); } 
  • something tells me that you need to commit commit transactions. - KoVadim

1 answer 1

Please specify what you are using as a database. Behavior is very similar to working with a local file base like Access or similar ... Studio, during compilation, each time it copies such base into the output directory (bin \ Release or bin \ Debug) rewriting the changes you made and works with it there. If this is the case, then it is treated in this way: in the Solution Explorer, right-click on the base and select Properties. Change "Copy to output directory" to "Copy if newer." And you need to look at the changed data in the output directory. I hope helped :)

  • I use a local database in a Windows Forms application. The property on "Copy if newer" changed. Still does not help. - ProgerStudent
  • Try replacing the using (connection = new SqlConnection (connectionString)) string with just SqlConnection connection = new SqlConnection (connectionString); - Oleg Knysh
  • And it seems to me that your insert is not correctly written ... string query = "INSERT INTO Users VALUES (@userName, @userAge)"; Must be string query = "INSERT INTO Users (UserName Field, UserAge Field) VALUES (@userName, @userAge)"; - Oleg Knysh
  • Interesting. Now new records appear in the table (in the DataGridView) even after the studio is restarted, but still do not appear in the table itself in the database. - ProgerStudent
  • Where are you looking? It is necessary to look in the database which is located in the output directory bin \ Release or bin \ Debug! - Oleg Knysh