×

ADO.NET 对 Microsoft SQL Server 的事务逻辑

Kalet Kalet 发表于2009-03-20 12:00:14 浏览257 评论0

抢沙发发表评论



ADO.NET 对 Microsoft SQL Server 的事务逻辑
Using connection As SqlConnection = New SqlConnection(connectString)
connection.Open()

' Start a local transaction.
Dim sqlTran As SqlTransaction = connection.BeginTransaction()

' Enlist the command in the current transaction.
Dim command As SqlCommand = connection.CreateCommand()
command.Transaction = sqlTran

Try
command.CommandText = _ADO.NET 对 Microsoft SQL Server 的事务逻辑
"INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')"
command.ExecuteNonQuery()
command.CommandText = _
"INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')"
command.ExecuteNonQuery()
sqlTran.Commit()
Console.WriteLine("Both records were written to database.")
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.WriteLine("Neither record was written to database.")
sqlTran.Rollback()ADO.NET 对 Microsoft SQL Server 的事务逻辑
End Try
End Using


群贤毕至

访客