Исходный код : Dapper / SqlMapper.cs на главном сервере · StackExchange / Dapper
Dapper QueryFirst Core Demo
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;
}
}
}
}
Почему чтение 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;
}
}
}
}