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

Bấm vào đây để xem trước nội dung
TÀI LIỆU LIÊN QUAN
5    176    1
5    255    1
5    106    0
5    121    1
6    103    1
6    107    1
6    121    1
6    103    0
6    140    0
6    98    0
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.