我在执行源代码时遇到此错误。但似乎我无法在互联网上获得太多信息。因此,我希望可以从这里的专业人士那里得到一些线索。
源代码
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:当前的TransactionScope已经完成 在System.Transactions.Transaction.get_Current()
在System.Data.ProviderBase.DbConnectionPool.GetFromTransactedPool(交易和交易)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&amp; connection)
在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
1重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal&amp; connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource1 retry, DbConnectionOptions userOptions)
1重试,DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
在System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)
1重试)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
在System.Data.SqlClient.SqlConnection.Open()
答案 0 :(得分:0)
此行OpenConnection
有点嗅觉,我猜你的问题就在这里
try
{
OpenConnection();
...
这会更有意义
try
{
//OpenConnection();
conn.Open();
queryrunner.QueryList(sql, param)
}
finally
{
//CloseConnection();
conn.Close();
}
这样你就会强迫自己控制连接生命周期而不是试图变得棘手,然后在另一个环境中猜测它
如果你把它写成
using
声明它实际上更好,但我不是 确定您正在使用的框架和/或语法