Use the CommandText method of the SqlCommand class (to construct a delete 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 DeleteDB (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 "delete information" button to create an SQL statement (to delete the specified information ). Then, the statement is passed to the deleteDB method to delete 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 = "delete from t_people where tb_PID =" +
This. textBox1.Text. Trim (). ToString () + "";
If (DBC. deleteDB (strSql ))
{
MessageBox. Show ("OK ");
}
}
Else
{
MessageBox. Show ("Enter the id ");
}
} Note: the readers who are serious about it will display the instructions for adding, modifying, and deleting objects in the demo. The methods to operate on them are essentially the same, but the names are different. So I remind you to cut the code and be careful at the same time.