C # principles and practices of database transactions (2)

Source: Internet
Author: User
Based on the above understanding, we will start to write our first transaction processing. Program . We can write the following program skillfully:
// Dotran. csusing system;
Using system. Data;
Using system. Data. sqlclient;
Namespace aspcn
{
Public class dbtran
{
File: // execute Transaction Processing
Public void dotran ()
{
File: // create a connection and open it
Sqlconnection myconn = getconn ();
Myconn. open ();
Sqlcommand mycomm = new sqlcommand ();
Sqltransaction mytran = new sqltransaction ();
Try
{
Mycomm. Connection = myconn;
Mycomm. Transaction = mytran;
File: // locate the pubs Database
Mycomm. commandtext = "use pubs ";
Mycomm. executenonquery ();
File: // update data
File: // convert all computer books
Mycomm. commandtext = "Update roysched set royalty = royalty * 1.10 where title_id like 'pc % '";
Mycomm. executenonquery (); // submit the transaction
Mytran. Commit ();
}
Catch (exception ERR)
{
Throw new applicationexception ("transaction operation error, system information:" + err. Message );
}
Finally
{
Myconn. Close ();
}
}
File: // get data connection
Private sqlconnection getconn ()
{
String strsql = "Data Source = localhost; Integrated Security = sspi; user id = sa; Password = ";
Sqlconnection myconn = new sqlconnection (strsql );
Return myconn;
}
}
Public class test {public static void main ()
{
Dbtran trantest = new dbtran ();
Trantest. dotran ();
Console. writeline ("transaction processing has been completed successfully. ");
Console. Readline ();
}
}
}

Obviously, this program is very simple and we are very confident in compiling it. However, unexpected results make our sense of accomplishment disappear:
Error cs1501: the "0" parameter is not obtained when the "sqltransaction" method is overloaded.
Why? Note thatCode:

Sqltransaction mytran = new sqltransaction ();

Obviously, the problem lies here. In fact, the sqltransaction class does not have a public constructor, so we cannot create a variable of the sqltrancaction type. Before the transaction is processed, a variable of the sqltransaction type is required. It is also necessary to associate the variable with the transcation attribute of the sqlcommand class, but the initial method is more special. When initializing the sqltransaction class, you need to use the begintranscation () method of the sqlconnection class:

Sqltransaction mytran; mytran = myconn. begintransaction ();

This method returns a variable of the sqltransaction type. After the begintransaction () method is calledSQLStatement execution actions are considered part of the transaction mytran. At the same time, you can also specify the transaction isolation level and transaction name in the parameters of this method, such:

Sqltransaction mytran;
Mytran = myconn. begintransaction (isolationlevel. readcommitted, "sampletransaction ");

The concept of isolation level will be discussed later. Here we only need to remember how a transaction is started and associated with a specific data link.
Do not rush to understand what we have done in our transactions, and see this line:

Mytran. Commit ();

Yes, this is the transaction commit method. After the statement is executed Database The operation will take effect and will be maintained by the persistence mechanism of the Database Transaction-that is, the system will have a fatal error after this, and the impact of the transaction on the database will not disappear.
After modifying the above program, we can get the following code (to save space, the repetition has been omitted, please refer to the previous article ):
// Dotran. CS ......}
File: // execute Transaction Processing
Public void dotran ()
{
File: // create a connection and open it
Sqlconnection myconn = getconn ();
Myconn. open ();
Sqlcommand mycomm = new sqlcommand ();
File: // sqltransaction mytran = new sqltransaction ();
File: // note that the sqltransaction class has no public Constructor
Sqltransaction mytran;
File: // create a transaction
Mytran = myconn. begintransaction ();
Try
{
File: // from then on, data operations based on the connection are considered as part of the transaction.
File: // bind the following connection and transaction object
Mycomm. Connection = myconn;
Mycomm. Transaction = mytran; file: // locate the pubs Database
Mycomm. commandtext = "use pubs ";
Mycomm. executenonquery (); // update data
File: // convert all computer books
Mycomm. commandtext = "Update roysched set royalty = royalty * 1.10 where title_id like 'pc % '";
Mycomm. executenonquery ();
File: // submit the transaction
Mytran. Commit ();
}
Catch (exception ERR)
{
Throw new applicationexception ("transaction operation error, system information:" + err. Message );
}
Finally
{
Myconn. Close ();
}
}
......

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.