Hello everyone. Please help me figure it out. I have a form . And there is a table in the database

How can I make the program so that when you click on the "Update" button, the program will enter the data in the appropriate fields? I tried to do it myself, but it didn't work out: That's what I tried to do.

MySqlCommand cmd = new MySqlCommand("UPDATE Settings SET sitename = '" + "" + "', keywords = '" + textBox2.Text + "', description = '" + textBox3.Text + "', seconds = '" + numericUpDown1.Value + "', domen = '" + textBox1.Text + ");"); 

3 answers 3

domen = '"+ textBox1.Text +"); "

There is not enough closing quotes. Look in the debugger how the request looks.

  • Which debugger? - Angus123
  • In the magic debugger that you will find in Wonderland: D - Zowie
  • Assumed that in VS there is something like that. If I made a mistake, I apologize. - msi
  • It exists, but not according to SQL'u ... and I did not find an error in SQL, but my code does not work, what should I do? Can the full version of the code? - Angus123
  • UPDATE MyTable Set Field1 = 'dogs', field2 = 'cats' WHERE SomeFilter = SomeValue; Do you have the same request code? not! correct: UPDATE Settings SET sitename = 'SomeSiteName', keywords = 'SomeKeyword', description = 'SomeDesc', seconds = 100500, domen = 'SomeDomen'; - Specter

From the studio debugger UPDATE Settings SET sitename = '', keywords = 'textbox2', description = 'textbox3', seconds = 'numericUpDown1', domen = 'textbox1); it is clear that the closing quote is missing. Therefore, it will be correct to:

 MySqlCommand cmd = new MySqlCommand("UPDATE Settings SET sitename = '" + "" + "', keywords = '" + textBox2.Text + "', description = '" + textBox3.Text + "', seconds = '" + numericUpDown1.Value + "', domen = '" + textBox1.Text + "');"); 
  • I now have another error ... rghost.ru/35574713/image.png - Angus123
  • Apparently, you need to specify on which connection the command is executed. - msi
  • Duck in my other teams, everything works, namely, on UPDATE does not want ... - Angus123

You have no connection to the database here. Create a connection:

 SqlConnection myConnection = new SqlConnection("user id="+this.user.ToString()+";" + "password="+this.password.ToString()+";server="+this.server.ToString()+";" + "Trusted_Connection="+this.Trusted_Connection.ToString()+";" + "database="+this.database.ToString()+"; " + "connection timeout="+this.timeout.ToString()); 

Variables specify yours. Then write:

  try { SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("Ваш запрос к базе", myConnection); myReader = myCommand.ExecuteReader(); } catch (Exception ee) { MessageBox.Show(ee.ToString()); }