gpt4 book ai didi

.net - 与 SMO 连接共享交易

转载 作者:行者123 更新时间:2023-12-02 00:09:24 26 4
gpt4 key购买 nike

如何与 SMO 服务器连接共享交易?共享连接就这么简单

var connection = new SqlConnection();
var serverConnection = new ServerConnection(connection);
var server = new Server(serverConnection);

但是如果连接已经有一个它自己的现有Transaction

var connection = new SqlConnection();
connection.BeginTransaction();

当我开始使用服务器对象时

var database = server.Databases[connection.Database];

这会被抛出:

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction

最佳答案

我遇到了同样的问题,也无法让它工作。

我的解决方法是改用 SQL 创建事务。这允许我在 SMO 和 ADO.NET 中使用相同的连接和事务。

Dim srvcn As New ServerConnection(sqlcn)
Dim srv As New Server(srvcn)

Dim sql As String = "SET CONCAT_NULL_YIELDS_NULL, ANSI_NULLS, ANSI_PADDING, QUOTED_IDENTIFIER, ANSI_WARNINGS, ARITHABORT, XACT_ABORT ON " & vbCrLf & _
"SET NUMERIC_ROUNDABORT, IMPLICIT_TRANSACTIONS, NOEXEC OFF " & vbCrLf & _
"GO " & vbCrLf & _
"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE " & vbCrLf & _
"GO " & vbCrLf & _
"BEGIN TRANSACTION" & vbCrLf & _
"GO "

srv.ConnectionContext.ExecuteNonQuery(sql)

完成交易也是通过纯 SQL 完成的:

srv.ConnectionContext.ExecuteNonQuery("IF @@TRANCOUNT>0 COMMIT TRANSACTION " & vbCrLf & _
"GO " & vbCrLf & _
"SET NOEXEC OFF " & vbCrLf & _
"GO ")

我希望这可能对你有所帮助,即使这个问题已经很老了......

关于.net - 与 SMO 连接共享交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16272818/

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