@@ -697,6 +697,7 @@ public static GridReader QueryMultiple(this IDbConnection cnn, CommandDefinition
697
697
{
698
698
return QueryMultipleImpl ( cnn , ref command ) ;
699
699
}
700
+
700
701
private static GridReader QueryMultipleImpl ( this IDbConnection cnn , ref CommandDefinition command )
701
702
{
702
703
object param = command . Parameters ;
@@ -710,7 +711,7 @@ private static GridReader QueryMultipleImpl(this IDbConnection cnn, ref CommandD
710
711
{
711
712
if ( wasClosed ) cnn . Open ( ) ;
712
713
cmd = command . SetupCommand ( cnn , info . ParamReader ) ;
713
- reader = cmd . ExecuteReader ( wasClosed ? CommandBehavior . CloseConnection | CommandBehavior . SequentialAccess : CommandBehavior . SequentialAccess ) ;
714
+ reader = cmd . ExecuteReader ( GetBehavior ( wasClosed , CommandBehavior . SequentialAccess ) ) ;
714
715
715
716
var result = new GridReader ( cmd , reader , identity , command . Parameters as DynamicParameters , command . AddToCache ) ;
716
717
cmd = null ; // now owned by result
@@ -749,7 +750,7 @@ private static IEnumerable<T> QueryImpl<T>(this IDbConnection cnn, CommandDefini
749
750
cmd = command . SetupCommand ( cnn , info . ParamReader ) ;
750
751
751
752
if ( wasClosed ) cnn . Open ( ) ;
752
- reader = cmd . ExecuteReader ( wasClosed ? CommandBehavior . CloseConnection | CommandBehavior . SequentialAccess : CommandBehavior . SequentialAccess ) ;
753
+ reader = cmd . ExecuteReader ( GetBehavior ( wasClosed , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult ) ) ;
753
754
wasClosed = false ; // *if* the connection was closed and we got this far, then we now have a reader
754
755
// with the CloseConnection flag, so the reader will deal with the connection; we
755
756
// still need something in the "finally" to ensure that broken SQL still results
@@ -811,7 +812,7 @@ private static T QueryFirstOrDefaultImpl<T>(this IDbConnection cnn, ref CommandD
811
812
cmd = command . SetupCommand ( cnn , info . ParamReader ) ;
812
813
813
814
if ( wasClosed ) cnn . Open ( ) ;
814
- reader = cmd . ExecuteReader ( wasClosed ? CommandBehavior . CloseConnection | CommandBehavior . SequentialAccess : CommandBehavior . SequentialAccess ) ;
815
+ reader = cmd . ExecuteReader ( GetBehavior ( wasClosed , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow ) ) ;
815
816
wasClosed = false ; // *if* the connection was closed and we got this far, then we now have a reader
816
817
817
818
T result = default ( T ) ;
@@ -1061,7 +1062,7 @@ static IEnumerable<TReturn> MultiMapImpl<TFirst, TSecond, TThird, TFourth, TFift
1061
1062
{
1062
1063
ownedCommand = command . SetupCommand ( cnn , cinfo . ParamReader ) ;
1063
1064
if ( wasClosed ) cnn . Open ( ) ;
1064
- ownedReader = ownedCommand . ExecuteReader ( wasClosed ? CommandBehavior . CloseConnection | CommandBehavior . SequentialAccess : CommandBehavior . SequentialAccess ) ;
1065
+ ownedReader = ownedCommand . ExecuteReader ( GetBehavior ( wasClosed , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult ) ) ;
1065
1066
reader = ownedReader ;
1066
1067
}
1067
1068
DeserializerState deserializer = default ( DeserializerState ) ;
@@ -1104,7 +1105,10 @@ static IEnumerable<TReturn> MultiMapImpl<TFirst, TSecond, TThird, TFourth, TFift
1104
1105
}
1105
1106
}
1106
1107
}
1107
-
1108
+ private static CommandBehavior GetBehavior ( bool close , CommandBehavior @default )
1109
+ {
1110
+ return close ? ( @default | CommandBehavior . CloseConnection ) : @default ;
1111
+ }
1108
1112
static IEnumerable < TReturn > MultiMapImpl < TReturn > ( this IDbConnection cnn , CommandDefinition command , Type [ ] types , Func < object [ ] , TReturn > map , string splitOn , IDataReader reader , Identity identity , bool finalize )
1109
1113
{
1110
1114
if ( types . Length < 1 )
@@ -1126,7 +1130,7 @@ static IEnumerable<TReturn> MultiMapImpl<TReturn>(this IDbConnection cnn, Comman
1126
1130
{
1127
1131
ownedCommand = command . SetupCommand ( cnn , cinfo . ParamReader ) ;
1128
1132
if ( wasClosed ) cnn . Open ( ) ;
1129
- ownedReader = ownedCommand . ExecuteReader ( ) ;
1133
+ ownedReader = ownedCommand . ExecuteReader ( GetBehavior ( wasClosed , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult ) ) ;
1130
1134
reader = ownedReader ;
1131
1135
}
1132
1136
DeserializerState deserializer = default ( DeserializerState ) ;
@@ -2272,8 +2276,7 @@ private static IDataReader ExecuteReaderImpl(IDbConnection cnn, ref CommandDefin
2272
2276
{
2273
2277
cmd = command . SetupCommand ( cnn , paramReader ) ;
2274
2278
if ( wasClosed ) cnn . Open ( ) ;
2275
- if ( wasClosed ) commandBehavior |= CommandBehavior . CloseConnection ;
2276
- var reader = cmd . ExecuteReader ( commandBehavior ) ;
2279
+ var reader = cmd . ExecuteReader ( GetBehavior ( wasClosed , commandBehavior ) ) ;
2277
2280
wasClosed = false ; // don't dispose before giving it to them!
2278
2281
disposeCommand = false ;
2279
2282
// note: command.FireOutputCallbacks(); would be useless here; parameters come at the **end** of the TDS stream
0 commit comments