This question has already been answered:

There is a certain datagrid to which the database is connected.
When you start the program and click the "Add" button, it successfully adds data to the table and after updating it shows my new added data. But once I close the program, and re-launch it, as my previously added data is not. I understand that the program adds them to the database, but as long as the program is running and after it is turned off, the data is not saved. On the Internet, everything is written in SQL and could not find the answer. Since this is c # on Visual Studio, and there, like, either they don’t like the visual, or they don’t hear something else.

private void button1_Click(object sender, EventArgs e) { // this.tableTableAdapter.Update(test_0DataSet3); try { this.Validate(); this.tableBindingSource.EndEdit();//разрешил изменения this.tableTableAdapter.Update(this.test_0DataSet3.Table); MessageBox.Show("Данные oбновлены!"); } catch(System.Exception ex) { MessageBox.Show("Ошибка обновления!"); } } 

PS There is a second code to add to the table, it is on sql, but also does not work. Here I used TextBox, but this is not important, it also adds data to the database until I turn off the program, then it does not save the data. The code is copied from another person who asked the question on this site and tried to do it himself. On the Internet, rummaged everything. There is no normal code that would correspond to what I need. I would just understand exactly how everything is done so that there are no further questions. I am not asking you to explain the code to me, I understand this a little. And I can read the documentation in the future. But I need it to work 100%, and try to understand the logic of the written. Help me please.

 using (SqlConnection con = new SqlConnection()) { con.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Test_0.mdf;Integrated Security=True;Connect Timeout=30"; con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Parameters.Clear(); cmd.Connection = con; int recordsAffected = cmd.ExecuteNonQuery(); if (recordsAffected == 0) { cmd.CommandText = "INSERT INTO [Table] ([Id], [Test1]) VALUES (@Id, @name)"; // Добавить параметры cmd.Parameters.AddWithValue("@Id", 654); cmd.Parameters.AddWithValue("@name", textBox1.Text);//Выполнить cmd.ExecuteNonQuery(); } con.Close(); } 

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

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 .

  • a small note using machine closes the connection, do not call Close() inside it Close() - Umed

1 answer 1

Faced the same problem for a long time. The problem was that Conection pointed to the database that was in the project folder. It either synchronizes it every time with the base we specify when adding a connection, or it’s a problem with setting up the base itself. Try in the ConnectionString to specify the full path to the database to which you originally connected.

I also helped to create a connection not to the database, but to the MSSQLSERVER itself (meaning the server where your database will be) in Visual Studio.