为什么Dapper QueryFirst Core DataReader读到结尾而不是只读一次

时间:2019-03-19 02:44:42

标签: c# dapper

源代码Dapper/SqlMapper.cs at master · StackExchange/Dapper

Dapper QueryFirst核心演示
using System.Data;
using System.Data.Common;

public static class DapperDemo
{
    public static T QueryFirstOrDefault<T>(this IDbConnection connection,string sql,Func<IDataRecord,T> selector)
    {
        using (var cmd = connection.CreateCommand())
        {
            cmd.CommandText = sql;
            using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
            {
                var data = (reader.Read() && reader.FieldCount != 0) ? selector(reader) : default(T);
                while (reader.Read()) { }
                while (reader.NextResult()) { }
                return data;
            }
        }
    }
}

enter image description here

为什么Dapper QueryFirst Core DataReader读到结尾而不是只读一次?

public static class DapperDemo
{
    public static T QueryFirstOrDefault<T>(this IDbConnection connection,string sql,Func<IDataRecord,T> selector)
    {
        using (var cmd = connection.CreateCommand())
        {
            cmd.CommandText = sql;
            using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
            {
                var data = (reader.Read() && reader.FieldCount != 0) ? selector(reader) : default(T); //Read only one time
                return data;
            }
        }
    }
}

0 个答案:

没有答案