Use the CommandText method of the SqlCommand class (to construct an update statement for it) and ExecuteNonQuery to modify table data.
The main program code is as follows. First, instantiate a SqlCommand object (cmd), and then use the SQL parameter (SQL statement) to create a cmd object. Finally, run the SQL statement using the ExecuteNonQuery METHOD OF THE cmd object.
Public bool updateDB (String SQL)
{
Using (SqlCommand cmd = new SqlCommand ())
{
Try
{
Cmd. CommandText = SQL; // set the SQL statement
Cmd. Connection = ConDB (); // call the method for opening the database Connection
Cmd. ExecuteNonQuery (); // Execute
Return true;
}
Catch
{
Return false;
}
}
} Click the "Modify information" button to create an SQL statement (to implement the modification function ). Then, pass the statement to the updateDB method to modify the data.
Private void button#click (object sender, EventArgs e)
{
If (this. textBox1.Text. Trim (). ToString ()! = "")
{
ClsDB. ClsDBControl DBC = new OptDB. ClsDB. ClsDBControl ();
String strSql = "update t_people set tb_1_x =" + this. textBox3.Text. Trim (). ToString () +
", Tb_pname =" + this. textBox2.Text. Trim (). ToString () + "where tb_PID =" +
This. textBox1.Text. Trim (). ToString () + "";
If (DBC. updateDB (strSql ))
{
MessageBox. Show ("OK ");
}
}
Else
{
MessageBox. Show ("Please write ID information ");
}
} Note: The idea should be strict. For example, make sure that the number information is not empty during modification. A better way is to verify whether the number to be changed exists first. In this way, the program is more rigorous.