如何使用 VB.net 在 sql server 中执行提交/回滚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25963445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do Commit/rollback in sql server using VB.net
提问by Sikander Hayyat
I'm working on an asp.net application which involves sql server as database. After writing huge function in vb.net, I had to select , insert and update different tables using one n other. I realized that, if all of this executed in one go then its a win win situation. If all of this doesn't go well then it would create a huge mess.
我正在开发一个涉及 sql server 作为数据库的 asp.net 应用程序。在 vb.net 中编写了巨大的函数后,我不得不选择、插入和更新不同的表。我意识到,如果所有这些都一次性执行,那么它就是一个双赢的局面。如果这一切都不顺利,那么就会造成巨大的混乱。
When we do DML operations in Oracle, we had to
当我们在 Oracle 中进行 DML 操作时,我们必须
commit;
rollback;
after every DML operation. My question is how do we do the same thing in sql server using VB.net.
在每次 DML 操作之后。我的问题是我们如何使用 VB.net 在 sql server 中做同样的事情。
My search leads to write a procedure @sql server. Inseration and delation will be done via sorted procedure. But I want it as normal operations like
我的搜索导致编写一个过程@sql server。插入和删除将通过排序过程完成。但我希望它像正常操作一样
SqlCommand("some query", connection")
Is it possible to do commit or rollback without using sorted procedures??
是否可以在不使用排序过程的情况下进行提交或回滚?
Thanks in advance!
提前致谢!
回答by Rhumborl
You can also use a TransactionScope, which gives a bit cleaner code than managing transactions yourself.
您还可以使用TransactionScope,它提供的代码比自己管理事务更简洁。
Using transaction As NewTransactionScope()
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As SqlCommand = connection.CreateCommand()
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
command.ExecuteNonQuery()
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
command.ExecuteNonQuery()
End Using
' If we do not run Commit(), e.g. an error occurs before we get here,
' the transaction will automatically roll back when we leave the Using block below
transaction.Commit()
End Using
回答by ne1410s
You should use SqlTransaction.
Here is a shameless copy-paste from MSDN:
你应该使用SqlTransaction. 这是来自 MSDN 的无耻复制粘贴:
Private Sub ExecuteSqlTransaction(ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As SqlCommand = connection.CreateCommand()
Dim transaction As SqlTransaction
' Start a local transaction
transaction = connection.BeginTransaction("SampleTransaction")
' Must assign both transaction object and connection
' to Command object for a pending local transaction.
command.Connection = connection
command.Transaction = transaction
Try
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
command.ExecuteNonQuery()
command.CommandText = _
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
command.ExecuteNonQuery()
' Attempt to commit the transaction.
transaction.Commit()
Console.WriteLine("Both records are written to database.")
Catch ex As Exception
Console.WriteLine("Commit Exception Type: {0}", ex.GetType())
Console.WriteLine(" Message: {0}", ex.Message)
' Attempt to roll back the transaction.
Try
transaction.Rollback()
Catch ex2 As Exception
' This catch block will handle any errors that may have occurred
' on the server that would cause the rollback to fail, such as
' a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType())
Console.WriteLine(" Message: {0}", ex2.Message)
End Try
End Try
End Using
End Sub

