A transaction (Transaction) is used to set the number of consecutive program operations that must be performed successfully, otherwise you must immediately reply to a state that does not perform any program operations
There are four properties in the four property transactions of a transaction, and these four properties are called acid. Acid refers to four properties such as non-partial-completion (atomicity) consistency (consistency) isolation (isolation) and durability (durability).
First build a demo database
--Build LibraryIF EXISTS(SELECTName fromsys.databasesWHEREName=N'Transtestdb') Drop Database [Transtestdb]CREATE DATABASE [Transtestdb];--Build Table Use [Transtestdb]GoIF EXISTS(SELECT * fromSys.objectsWHERE object_id = object_id(N'[dbo]. [Transtesttable]') andTypeinch(N'U')) Drop Table [transtesttable]CREATE TABLE [dbo].[transtesttable](Idint,[Name] varchar( -));--Initial value Use [Transtestdb]GoInsert into [transtesttable] Select 1,'a' Union Select 2,'b' Union Select 3,'C';
(1) implemented directly in the SQL statement.
beginTrybegin Tran Insert intoDbo. TranstesttableValues( the,' the'); UpdateDbo. TranstesttableSet [Name] = ' the' where [Id] = the; --RAISERROR (' Error raised in TRY block. ', 16,1); Commit TranEndTrybeginCatchrollback TranEndCatch
(2) using the object SqlTransaction implementation, generally through the SqlConnection object 's BeginTransaction () method to create, Of course, you can create a SqlTransaction transaction object first, and then set the Connection property.
Public Static intExecutesqltran (list<string>sqlstringlist) { using(SqlConnection conn =NewSqlConnection (connectionString)) {Conn. Open (); SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; SqlTransaction TX=Conn. BeginTransaction (); Cmd. Transaction=TX; Try { intCount =0; for(intn =0; n < Sqlstringlist.count; n++) { stringstrSQL =Sqlstringlist[n]; if(strSQL. Trim (). Length >1) {Cmd.commandtext=strSQL; Count+=cmd. ExecuteNonQuery (); }} tx.commit (); returncount; } Catch{TX. Rollback (); return 0; } } }
The SqlTransaction object has several common methods of commit (), Rollback(), Rollback(String) , and Save (string).
Commit (): Indicates that the transaction has completed execution and executes a commit in the same SQL statement.
rollback () : rolls back the transaction from the suspended state.
Save (string), Rollback(String) : These two methods are generally used to create a savepoint in the process of a transaction, and the latter represents a rollback to the specified savepoint.
(3) The TransactionScope object implements transaction control, and this object has a complete method that indicates that the transaction was completed. There is no other transaction operation function, which is generally placed after the using directive, and if an exception or error occurs, it is rolled back directly.
Public Static intExecutesqltran (list<string>sqlstringlist) { using(TransactionScope scope =NewTransactionScope ()) {SqlConnection conn=NewSqlConnection (connectionString) Sqlconn.open (); SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; intCount =0; for(intn =0; n < Sqlstringlist.count; n++) { stringstrSQL =Sqlstringlist[n]; if(strSQL. Trim (). Length >1) {Cmd.commandtext=strSQL; Count+=cmd. ExecuteNonQuery (); }} tx.commit (); Sqlconn.close (); Scope.complete (); } }
Database transaction Control