gpt4 book ai didi

C# System.InvalidOperationException : The current TransactionScope is already complete

转载 作者:行者123 更新时间:2023-12-04 13:03:20 37 4
gpt4 key购买 nike

我在执行我的源代码时遇到了这个错误。但似乎我在互联网上无法获得太多信息。因此,我希望可以从这里的专业人士那里得到一些线索。

源代码

public List<string> GetData (List<long> Id)
{
List<string> data;
string sql = "select * from tblSample with(nolock) where SampleId in @sampleId";
Dapper.DynamicParameters param = new Dapper.DynamicParameters();
param.Add("@sampleId", Id);

try
{
data = this.queryrunner.QueryList(sql, param);
}
catch (Exception ex)
{
logger.Error(ex.Message, ex);
}

return data;
}

QueryRunner 类方法
    private void OpenConnection()
{
if (this.conn.State != ConnectionState.Open)
{
this.conn.Open();
}
}

public List<t> QueryList(String sql, DynamicParameters param)
{
List<t> t;

try
{
OpenConnection();
t = this.conn.Query<t>(sql, param).ToList();
}
catch (System.Data.SqlClient.SqlException ex)
{
logger.Debug(ex.Message, ex);
LogSqlErrorException(this.conn.Database, sql, param);
throw;
}
finally
{
CloseConnection();
}

return t;
}

这是错误日志的一部分:-

System.InvalidOperationException: The current TransactionScope is already complete.
at System.Transactions.Transaction.get_Current()
at System.Data.ProviderBase.DbConnectionPool.GetFromTransactedPool(Transaction& transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)

at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)

at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)

at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry)
at System.Data.SqlClient.SqlConnection.Open()

最佳答案

此行 OpenConnection有点味道,我猜这就是你的问题所在

try
{
OpenConnection();
...
会更有意义
try
{
//OpenConnection();
conn.Open();
queryrunner.QueryList(sql, param)
}
finally
{
//CloseConnection();
conn.Close();
}
这样你就强制自己控制连接生命周期,而不是试图在另一个上下文中狡猾和第二次猜测它

Its actually better if you put it a using statement, however i'm notsure of the framework you are using, and/or the syntax

关于C# System.InvalidOperationException : The current TransactionScope is already complete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48615425/

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