Use transactions (WinForm + SQL Server)

Source: Internet
Author: User

A transaction is a series of operations performed as a single logical unit of work. A logical unit of work must have four attributes, called ACID (atomicity, consistency, isolation, and durability). Only in this way can a transaction be made. The transaction management feature forces the atomicity and consistency of transactions. After the transaction is started, it must be completed successfully. Otherwise, SQL Server will cancel all modifications made to the data after the transaction is started.

Out of the ACID nature of transactions, the author uses the consistency of application transactions to exclude a series of "dangerous" factors that may occur during the running process of the program to ensure data security. For example, when a user withdraws money, he or she is stuck in a cash machine and waits for the money to be withdrawn. In this case, a sudden power failure occurs. The transaction can help the user recover the loss and restore the information to the previous state. The main program code is as follows. First, instantiate a connection object, and then instantiate a SqlTransaction object sqlTran and a SqlCommand object command through the connection object, and then associate the Transaction attribute of the command object with the sqlTran object, implement program consistency through things.

Public bool Tran (string strID, string strName, string strSex, string strDelID, bool IfDO)
{
Using (SqlConnection connection = ConDB (); // create a connection object
{
SqlTransaction sqlTran = connection. BeginTransaction (); // start the transaction
SqlCommandcommand = connection. CreateCommand (); // create a SqlCommand object
Command. Transaction = sqlTran; // associate SqlCommand with SqlTransaction
Try
{
Command. CommandText = "insert into t_people values (" + strID + "," + strName +
"," + StrSex + ")";
Command. ExecuteNonQuery ();
Command. CommandText = "delete from t_people where tb_PID =" + strDelID + "";
Command. ExecuteNonQuery ();
If (IfDO) // throws an exception and rolls back
{
Throw new Exception ();
}
SqlTran. Commit ();
Connection. Close ();
Return true;
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
SqlTran. Rollback ();
Return false;
}
}
} Is used to obtain parameters for the Tran method. If the Tran method is successful, the information is successfully added. Otherwise, the addition fails. In order to allow the reader to better understand the working principles of transactions, the operation method (commit and rollback) of manually identified transactions in the program ).

Private void button#click (object sender, EventArgs e)
{
ClsDB. ClsDBControl DBDT = new OptDB. ClsDB. ClsDBControl ();
String strid, strname, strsex, strDelid;
Strid = this. textBox1.Text. Trim (). ToString ();
Strname = this. textBox2.Text. Trim (). ToString ();
Strsex = this. textBox3.Text. Trim (). ToString ();
StrDelid = this. textBox4.Text. Trim (). ToString ();
Bool Bt;
If (this. radioButton1.Checked)
{
Bt = false;
}
Else
{
& Nb

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.