本文介绍了使用插入查询回滚错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码。
Hi, I Have the following Code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sqlcmd As New SqlClient.SqlCommand Dim con As New SqlClient.SqlConnection("Data Source=kgmmurugesh\sqlexpress;Initial Catalog=KGMERP_STORES;Integrated Security=True") Try con.Open() sqlcmd.Connection = con sqlcmd.CommandText = "begin tran ItemCreation" sqlcmd.ExecuteNonQuery() sqlcmd.CommandText = "Insert into Item_table(Item_Name,Item_PRate) values ('apple','a150')" sqlcmd.ExecuteNonQuery() sqlcmd.CommandText = "commit tran ItemCreation" sqlcmd.ExecuteNonQuery() Catch ex As Exception sqlcmd.CommandText = "rollback tran ItemCreation" sqlcmd.ExecuteNonQuery() End Try End Sub它运行良好,但有时会显示以下错误: ROLLBACK TRANSACTION请求没有相应的BEGIN TRANSACTION。 我的代码中有什么错误?
It works well, but sometimes it shows the following error: The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. what is the mistake in my code?
推荐答案回滚似乎不是原始交易的一部分,它是在一个单独的sqlcmd The rollback doesn't seem to be part of the original transaction, it's on a separate sqlcmd
成功时需要执行SqlTransaction.Commit()。 $ C $ b和Catch中的SqlTransaction.Rollback()。 请参考以下链接:提交和 回滚 You need to perform SqlTransaction.Commit() when success. and SqlTransaction.Rollback() in Catch. Refer this links : Commit and Rollback
您需要以下内容: You would need something along the lines of: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sqlcmd As New SqlClient.SqlCommand Dim con As New SqlClient.SqlConnection("Data Source=kgmmurugesh\sqlexpress;Initial Catalog=KGMERP_STORES;Integrated Security=True") con.Open() sqlcmd.Connection = con sqlcmd.CommandText = @"begin tran ItemCreation; insert into Item_table(Item_Name,Item_PRate) values ('apple','a150') if @@error <> 0 rollback tran ItemCreation; else commit tran ItemCreation; " sqlcmd.ExecuteNonQuery() End Sub
SQL没问题,但我现在没有打开Visual Studio,所以我不确定关于VB。事实是,你的VB代码不知道你的事务是否已提交或回滚
The SQL is OK but I don't have Visual Studio open right now so I'm not sure about the VB. Thing is, your VB code won't know if your transaction has committed or rolled back
更多推荐
使用插入查询回滚错误
发布评论