gpt4 book ai didi

C# SqlTransaction.Commit 抛出异常,因为 Connection 为空

转载 作者:太空宇宙 更新时间:2023-11-03 11:22:24 25 4
gpt4 key购买 nike

我在使用 SqlTransaction 时遇到问题。这是代码

SqlConnection conn = new SqlConnection(connectionString);
// assume that at this point I add try/catch and the connection is successfully opened.
conn.Open();
SqlTransaction trans = conn.BeginTransaction(IsolationLevel.RepeatableRead);

//.........
// Here I do my action with database, change it, commit, call ExecuteNonQuery, //ExecuteScalar,etc...

// And after that long processing with this transaction, I don't know why trans.Connection //is null. and therefore when I call trans.commit(), an exception was raised?


trans.commit();

有没有人有任何线索?

我认为这可能是因为数据库服务器的内存因连接过多而被耗尽。但我不确定。

我该如何解决?

最佳答案

利用利用

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
Console.WriteLine("State: {0}", connection.State);
}

关于C# SqlTransaction.Commit 抛出异常,因为 Connection 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549779/

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