Dapper对于数据库连接的管理:如果已经打开,它会关闭连接。如果你只是做一个快速查询-让Dopter自己处理它。

  如果你做了很多事情,你应该自己打开连接,并在最后关闭连接,所有的查询在中…只是从效率的角度来看。

 

bool wasClosed = cnn.State == ConnectionState.Closed;
using (var cmd = (DbCommand)command.SetupCommand(cnn, info.ParamReader))
{
    try
    {
        if (wasClosed) await ((DbConnection)cnn).OpenAsync(command.CancellationToken).ConfigureAwait(false);
        var result = await cmd.ExecuteNonQueryAsync(command.CancellationToken).ConfigureAwait(false);
        command.OnCompleted();
        return result;
    }
    finally
    {
        if (wasClosed) cnn.Close();
    }
}

其实dapper对连接的打开关闭已经处理的很好,大多数情况下我们并不需要去另外做处理,只管调用它提供的方法即可。

相关文章:

  • 2022-12-23
  • 2021-07-14
  • 2021-07-10
  • 2022-12-23
  • 2021-12-21
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-03
  • 2021-10-08
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案