Delete database data (SQL Server)

Source: Internet
Author: User

 

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.