tailieunhanh - Microsoft ADO .NET 4 Step by Step - p 23
Thay vì tạo ra các trường hợp SqlTransaction trực tiếp, bạn tạo kết nối giao dịch cụ thể bằng cách sử dụng phương pháp BeginTransaction đối tượng SqlConnection của. Giao dịch chỉ làm việc trên các kết nối cơ sở dữ liệu mở, cho nên bạn phải gọi phương thức Open của đối tượng kết nối đầu tiên. | 196 Microsoft 4 Step by Step Instead of creating instances of SqlTransaction directly you generate connection-specific transactions using the SqlConnection object s BeginTransaction method. Transactions work only on open database connections so you must call the connection object s Open method first. C using SqlConnection linkToDB new SqlConnection connectionString SqlTransaction envelope Visual Basic Using linkToDB As SqlConnection New SqlConnection connectionString Dim envelope As SqlTransaction After obtaining a transaction object add it to any SqlCommand objects that should be part of the transaction. C ----- Include the transaction in the SqlCommand constructor. SqlCommand updateCommand new SqlCommand sqlText linkToDB envelope ----- Or add it to an existing SqlCommand object. SqlCommand updateCommand new SqlCommand sqlText linkToDB envelope Visual Basic ------ Include the transaction in the SqlCommand constructor. Dim updateCommand As New SqlCommand sqlText linkToDB envelope ------ Or add it to an existing SqlCommand object. Dim updateCommand As New SqlCommand sqlText linkToDB envelope After you ve issued all the transaction-specific commands you can commit or roll back the entire transaction by calling the SqlTransaction object s Commit or Rollback method. Chapter 12 Guaranteeing Data Integrity 197 C ---- Commit the transaction. ---- Rollback the transaction. Visual Basic ----- Commit the transaction. ----- Rollback the transaction. You should always call Commit or Rollback explicitly. If you dispose of the object or allow it to go out of scope without calling one of these two methods the transaction will be rolled back but at a time determined by the .NET garbage collection system. Both Commit and Rollback and the initial BeginTransaction call as well .
đang nạp các trang xem trước