Transaction is a program execution unit that accesses and may update various data items in the database ). Transactions are usually caused by the execution of user programs written in advanced database manipulation languages or programming languages (such as SQL, C ++, or Java), and are formatted as begin transaction and commit transaction.
Or rollback transaction statement (or callback function. A transaction is composed of all operations performed between the beginning (transin transaction) and the end (committransaction or rollback transaction) of the transaction. In SQL Server, transaction can be abbreviated as tran at the beginning or end of a transaction statement.
Things should have four attributes: atomicity, consistency, isolation, and continuity. These four attributes are generally called ACID properties.
Atomicity: a transaction is an inseparable unit of work. All operations in a transaction are either done or not done.
Consistency: A transaction must change the database from one consistent state to another. Consistency is closely related to atomicity.
Isolation: the execution of a transaction cannot be disturbed by other transactions. That is to say, operations and data used within a transaction are isolated from other concurrent transactions, and each transaction executed concurrently cannot interfere with each other.
Durability: permanence refers to a transaction that changes data in the database once committed. Other subsequent operations or faults should not have any impact on them.
Start the service
Distributed Transaction Coordinator (TrkWks)
Add reference
System. Transactions
Introduce namespace
Using System. Transactions;
Write code
Static void Main (string [] args) {// distributed transaction (atomicity, either all succeeded or all failed (rollback → SQL completed )) using (TransactionScope ts = new TransactionScope () {// Step 1 string SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "12" }}; Console. writeLine (SqlHelper. executeScalar (SQL, param ). toString (); // Step 2: string _ SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] _ param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "1"}; Console. writeLine (SqlHelper. executeScalar (_ SQL, _ param ). toString (); // complete ts. complete ();} Console. readKey ();} static void Main (string [] args) {// distributed transaction (atomicity, either all succeeded or all failed (rollback → SQL completed )) using (TransactionScope ts = new TransactionScope () {// Step 1 string SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "12" }}; Console. writeLine (SqlHelper. executeScalar (SQL, param ). toString (); // Step 2: string _ SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] _ param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "1"}; Console. writeLine (SqlHelper. executeScalar (_ SQL, _ param ). toString (); // complete ts. complete ();} Console. readKey ();}
Ts. Complete (); Transaction completion ts. Complete (); Transaction completion