在 ExecuteScalar 中使用两个查询时,一个查询可能会失败

发布于 11-10 18:57 字数 142 浏览 7 评论 0原文

我编写了一个查询,它是一个复合查询,例如一个更新查询和一个插入查询。 我正在使用 ExecuteScalar。 是否有可能一个查询失败而另一个查询成功。 如果其中一个查询错误,将导致整个失败或部分失败。

有人可以帮忙:)

I have written a query which is a composite query like one update query and one insert query is there.
I am using ExecuteScalar.
Is there a possibility that one query will fail and other will be succeeded.
It will be a whole failure or partial failure, if one of the queries is wrong.

can someone please help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔书几行2024-11-17 18:57:47

无论您如何调用它;对于复合操作,完全有可能第一个操作成功而第二个操作失败(例如,唯一键冲突或外键冲突)。如果您需要确保全有或全无,请用事务包围对 Execute* 的调用,并且仅在您知道一切正常时才提交。

您还可以在 SQL 级别创建事务,但如果您在 TSQL 中创建事务,则您所使用的工具更容易出错。值得一提的是,它确实避免了几次往返——但通常不会太多,以至于值得与语言进行斗争来做到这一点。最终,SQL 是为良好的基于​​集合的 DML 操作而设计的 - 它不太适合像执行流管理这样的过程性代码。

No matter how you invoke it; for a composite operation it is entirely possible that the first operation will succeed and the second operation will fail (for example, a unique key violation or a foreign key violation). If you need to ensure all-or-nothing, then surround the call to Execute* with a transaction, and only commit when you know it all worked.

You can also create transactions at the SQL level, but the tools at your disposal are easier to get wrong if you do it in TSQL. It does, to give due credit, avoid a few round-trips - but usually not so much that it is worth fighting the language to do it. Ultimately SQL is designed for good set-based DML operations - it isn't quite as good at more procedural code like execution flow management.

三五鸿雁2024-11-17 18:57:47

是的,您的一个查询很可能失败,而其他查询则执行。以及为什么不使用 ExecuteNonQuery 而不是 ExecuteScalar。对于这些类型的场景,最好使用“事务”

Yes chances are there that your one query fails and other execute. and why not to use ExecuteNonQuery instead of ExecuteScalar. Its better you use "transactions" for these type of scenarios

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文