SQL 语句中 SQLITE 中的错误处理和回滚事务 [英] Error handling and RollBack Transaction in SQLITE from SQL Statement

查看:30
本文介绍了SQL 语句中 SQLITE 中的错误处理和回滚事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过调用 执行非查询.我想在事务中执行此操作,并希望在出现任何故障时回滚.

I'm Altering multiple sqlite tables with SQL script by calling ExecuteNonQuery. I want to do this operation in transaction and want to roll it back when anything fails.

我查看了 BEGIN TRANSACTION 很明显我有当出现任何问题时调用 ROLLBACK TRANSACTION.但我不知道如何尝试...CATCH (Transact-SQL) 类似的东西.

I looked at BEGIN TRANSACTION and its clear that I have to call ROLLBACK TRANSACTION when anything goes wrong. But I don't know how could TRY...CATCH (Transact-SQL) kind of thing here.

注意:整个 Sql 脚本文件(除了需要在一个事务中触发的这几条语句之外,还包含许多其他语句)由 .ReadToEnd() 读取,然后立即执行.我想在 sql 脚本文件本身中处理这个问题,不想更改代码.

NOTE: Whole of Sql Script file (which contains many other statements apart from these few statements which needs to be fired in one transaction) is read by .ReadToEnd() and then executed in one go as of now. I want to handle this in sql script file itself and don't want to change the code.

推荐答案

请看SQLite 关于冲突子句

从开始交易开始

你必须在你的动作中添加ON CONFLICT ROLLBACK

You have to add ON CONFLICT ROLLBACK on your actions

最后是 COMMIT TRANSACTION ;-)

And COMMIT TRANSACTION at the end ;-)

这篇关于SQL 语句中 SQLITE 中的错误处理和回滚事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆