@@ -459,15 +459,15 @@ async Task<bool> NextResult(bool async, bool isConsuming = false, CancellationTo
459459 continue ;
460460 }
461461
462- if ( ! Command . IsWrappedByBatch && StatementIndex == 0 && Command . _parameters ? . HasOutputParameters == true )
462+ if ( ( Command . IsWrappedByBatch || StatementIndex is 0 ) && Command . InternalBatchCommands [ StatementIndex ] . _parameters ? . HasOutputParameters == true )
463463 {
464- // If output parameters are present and this is the first row of the first resultset,
464+ // If output parameters are present and this is the first row of the resultset,
465465 // we must always read it in non-sequential mode because it will be traversed twice (once
466466 // here for the parameters, then as a regular row).
467467 msg = await Connector . ReadMessage ( async ) . ConfigureAwait ( false ) ;
468468 ProcessMessage ( msg ) ;
469469 if ( msg . Code == BackendMessageCode . DataRow )
470- PopulateOutputParameters ( ) ;
470+ PopulateOutputParameters ( Command . InternalBatchCommands [ StatementIndex ] . _parameters ! ) ;
471471 }
472472 else
473473 {
@@ -598,12 +598,11 @@ async ValueTask ConsumeResultSet(bool async)
598598 }
599599
600600
601- void PopulateOutputParameters ( )
601+ void PopulateOutputParameters ( NpgsqlParameterCollection parameters )
602602 {
603603 // The first row in a stored procedure command that has output parameters needs to be traversed twice -
604604 // once for populating the output parameters and once for the actual result set traversal. So in this
605605 // case we can't be sequential.
606- Debug . Assert( StatementIndex == 0 ) ;
607606 Debug . Assert ( RowDescription != null ) ;
608607 Debug . Assert ( State == ReaderState . BeforeResult ) ;
609608
@@ -616,7 +615,7 @@ void PopulateOutputParameters()
616615 var taken = new List < NpgsqlParameter > ( ) ;
617616 for ( var i = 0 ; i < ColumnCount; i++ )
618617 {
619- if ( Command . Parameters . TryGetValue ( GetName ( i ) , out var p) && p . IsOutputDirection )
618+ if ( parameters . TryGetValue ( GetName ( i ) , out var p) && p . IsOutputDirection )
620619 {
621620 p . Value = GetValue ( i ) ;
622621 taken . Add ( p ) ;
@@ -628,7 +627,7 @@ void PopulateOutputParameters()
628627 // Not sure where this odd behavior comes from: all output parameters which did not get matched by
629628 // name now get populated with column values which weren't matched. Keeping this for backwards compat,
630629 // opened #2252 for investigation.
631- foreach ( var p in ( IEnumerable < NpgsqlParameter > ) Command . Parameters )
630+ foreach ( var p in ( IEnumerable < NpgsqlParameter > ) parameters )
632631 {
633632 if ( ! p. IsOutputDirection || taken. Contains( p) )
634633 continue ;
0 commit comments