This question has already been answered:

Problem solved! Answer: When you start from the bin folder, the entered data is saved! So I make changes:

string connStr = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LitMapPoltavaData.mdf;Integrated Security=True"; SqlConnection conn = new SqlConnection(connStr); try { //пробуем подключится conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Parameters.Clear(); cmd.Connection = conn; cmd.CommandText = "UPDATE Writers SET Id = @Id, Surname = @Surname, Name = @Name, Lived = @Lived, Birthplace = @Birthplace, ShortInfo = @ShortInfo WHERE Id = @Id"; //Добавить параметры ... //Выполнить int recordsAffected = cmd.ExecuteNonQuery(); if (recordsAffected == 0) { cmd.CommandText = "Insert Into Writers (Id, Surname, Name, Lived, Birthplace, ShortInfo)" + "Values(@Id, @Surname, @Name, @Lived, @Birthplace, @ShortInfo)"; cmd.ExecuteNonQuery(); } } finally { conn.Close(); conn.Dispose(); } 

The data in the database are entered and displayed after normal. It is necessary that after closing and opening a new application, the data in the database remains!

Reported as a duplicate by PashaPash member c # Jan 25 '17 at 6:36 am

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • The question is not quite clear, why should they wear out? - Alexsandr Ter
  • commit is more to SVN - Alexsandr Ter
  • "It is necessary that ... the data in the database remain" And they do not remain with you? The code seems more or less. You do not open a transaction, respectively, the commit will occur at the level of the individual statement, i.e. there is no need to specifically commit. - i-one
  • update makes changes to the database immediately (you do not use explicit transaction management). most likely you simply restart the application from under the studio, via F5, and it re-copies the empty LitMapPoltavaData.mdf into bin \ LitMapPoltavaData.mdf, milling the changes. Try running the application directly from bin. - PashaPash
  • @PashaPash, in Oracle without an explicit commit, commit does not occur. - pegoopik

1 answer 1

UPDATE in SQL Server makes changes to the database immediately (you do not use explicit transaction management).

Most likely, you simply restart the application from under the studio, via F5, and it copies the empty LitMapPoltavaData.mdf back to bin \ LitMapPoltavaData.mdf, rubbing the changes.

Try running the application directly from bin.