Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Microsoft SQL Server 2005 Developer’s Guide- P23:SQL Server 2005 is a feature-rich release that provides a host of new tools and technologies for the database developer. This book is written to help database developers and DBAs become productive immediately with the new features and capabilities found in SQL Server 2005. | Chapter 6 Developing Database Applications with ADO.NET 199 functions would be considered part of the same logical transaction. From the database standpoint to ensure database integrity both the withdrawal and the deposit would be grouped together as a single transaction. If the withdrawal operation succeeded but the deposit failed the entire transaction could be rolled back which would restore the database to the condition it had before the withdrawal operation was attempted. Using transactions is an essential part of most production-level database applications. ADO.NET supports transactions using the Transaction classes. In order to incorporate transactions into your ADO.NET applications you first need to create an instance of the SqlTransaction object and then execute the BeginTransaction method to mark the beginning of a transaction. Under the covers this will cause the database server to begin a transaction. For instance using the SqlTransaction object to issue a BeginTransaction statement will send a T-SQL BEGIN TRANSACTION command to SQL Server. After the transaction has started the database update operations are performed and then the Commit method is used to actually write the updates to the target database. If an error occurs during the process then the RollBack operation is used to undo the changes. The following SQLCommandTransaction subroutine shows how to start a transaction and then either commit the results of the transaction to the database or roll back the transaction in the event of an error Private Sub SQLCommandTransaction cn As SqlConnection Dim cmd As New SqlCommand Dim trans As SqlTransaction Start a local transaction trans cn.BeginTransaction cmd.Connection cn cmd.Transaction trans Try Insert a row transaction cmd.CommandText _ INSERT INTO Department VALUES 100 Transaction 100 cmd.ExecuteNonQuery This next insert will result in an error cmd.CommandText _ INSERT INTO Department VALUES 100 Transaction 101 cmd.ExecuteNonQuery trans.Commit .