zoukankan      html  css  js  c++  java
  • Dapper的数据库连接管理(打开、关闭)

      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对连接的打开关闭已经处理的很好,大多数情况下我们并不需要去另外做处理,只管调用它提供的方法即可。
  • 相关阅读:
    谦卑
    自尊和自我效能
    二手时间读书笔记
    vim学习4
    vim学习3
    hdu 5122 K.Bro Sorting
    hdu 5113 Black And White
    poj 2479 Maximum sum
    poj 2392 Space Elevator
    poj 3666 Making the Grade
  • 原文地址:https://www.cnblogs.com/gougou1981/p/9581482.html
Copyright © 2011-2022 走看看