gpt4 book ai didi

c# - .NET TransactionScopeOption.Suppress 的 T-SQL 等价物

转载 作者:行者123 更新时间:2023-11-30 20:36:31 34 4
gpt4 key购买 nike

在我的 .NET 代码中,在数据库事务中(使用 TransactionScope ),我可以包含一个带有 TransactionScopeOption.Suppress 的嵌套 block ,这确保嵌套 block 内的命令甚至被提交如果外部 block 回滚。以下是代码示例:

using (TransactionScope txnScope = new TransactionScope(TransactionScopeOption.Required))
{
db.ExecuteNonQuery(CommandType.Text, "Insert Into Business(Value) Values('Some Value')");

using (TransactionScope txnLogging = new TransactionScope(TransactionScopeOption.Suppress))
{
db.ExecuteNonQuery(CommandType.Text, "Insert Into Logging(LogMsg) Values('Log Message')");
txnLogging.Complete();
}

// Something goes wrong here. Logging is still committed

txnScope.Complete();
}

我试图找出这是否可以在 T-SQL 中完成。有几个人推荐了OPENROWSET ,但使用起来看起来不是很“优雅”。此外,我认为将连接信息放在 T-SQL 代码中是个坏主意。

我过去使用过 SQL Service Broker,但它也支持事务性消息传递,这意味着在提交数据库事务之前不会将消息发布到队列。

我的要求:我们的应用程序存储过程由某些第三方应用程序在存储过程外部启动的隐式事务中触发。我希望能够在我的存储过程中捕获并记录任何错误(在同一数据库的数据库表中)。我需要重新抛出异常,让第三方应用程序回滚事务,并让它知道操作失败(并因此在失败的情况下执行所需的任何操作)。

最佳答案

您可以通过将remote proc transaction Promotion 选项设置为false 来设置环回链接服务器,然后在TSQL 中访问它或在SQL server 中使用CLR 过程在事务外创建新连接做你的工作。

How to create an autonomous transaction in SQL Server 2008 中建议的两种方法.

这两种方法都涉及创建新连接。有一个 open connect item请求 native 提供此功能。

关于c# - .NET TransactionScopeOption.Suppress 的 T-SQL 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36858081/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com