tailieunhanh - Introducing Transactions

Introducing Transactions In Chapter 3, you saw how you can group SQL statements together into transactions. The transaction is then committed or rolled back as one unit. | Introducing Transactions In Chapter 3 you saw how you can group SQL statements together into transactions. The transaction is then committed or rolled back as one unit. For example in the case of a banking transaction you might want to withdraw money from one account and deposit it into another. You would then commit both of these changes as one unit or if there s a problem roll back both changes. You ll be introduced to using transactions in in this section. There are three Transaction classes SqlTransaction OleDbTransaction and OdbcTransaction and you use an object of one of these classes to represent a transaction in . I ll show you how to use an object of the SqlTransaction class in this section. Let s consider an example transaction that consists of two INSERT statements. The first INSERT statement will add a row to the Customers table and the second one will add a row to the Orders table. The new row in the Orders table will reference the new row in the Customers table and the two INSERT statements are as follows INSERT INTO Customers CustomerID CompanyName VALUES J3COM Jason Price Corporation INSERT INTO Orders CustomerID VALUES J3COM You can use the following steps to perform these two INSERT statements using a SqlTransaction object 1. Create a SqlTransaction object and start the transaction by calling the BeginTransaction method of the SqlConnection object. 2. Create a SqlCommand object to hold the SQL statement. 3. Set the Transaction property for the SqlCommand object to the SqlTransaction object created in step 1. 4. Set the CommandText property of the SqlCommand object to the first INSERT statement. This INSERT statement adds a row to the Customers table. 5. Run the first INSERT statement using the ExecuteNonQuery method of the SqlCommand object. This method is used because an INSERT statement doesn t return a result set. 6. Set the CommandText property of the SqlCommand object to the second INSERT statement. This statement adds a row to

TÀI LIỆU LIÊN QUAN
10    158    1
6    184    1
7    162    1
5    157    1
6    160    1
6    152    1
6    150    1
6    206    1
7    154    1
5    121    1