使用插入查询回滚错误

编程入门 行业动态 更新时间:2024-10-27 21:10:47
本文介绍了使用插入查询回滚错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码。

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

更多推荐

使用插入查询回滚错误

本文发布于:2023-10-19 19:30:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508542.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!