@@ -44,6 +44,22 @@ public static void ExecuteReader(this SqlCommand command, Action<SqlDataReader>
44
44
handler ( reader ) ;
45
45
}
46
46
47
+ /// <param name="command">The <see cref="SqlCommand"/> to generate a reader from.</param>
48
+ /// <param name="transform">The transform function for each <see cref="SqlDataReader"/>.</param>
49
+ /// <param name="behavior">The behavior to use with the data reader.</param>
50
+ /// <inheritdoc cref="CommandExtensions.ExecuteReader{T}(IDbCommand, Func{IDataReader, T}, CommandBehavior)"/>
51
+ public static T ExecuteReader < T > ( this SqlCommand command , Func < SqlDataReader , T > transform , CommandBehavior behavior = CommandBehavior . Default )
52
+ {
53
+ if ( command is null ) throw new ArgumentNullException ( nameof ( command ) ) ;
54
+ if ( transform is null ) throw new ArgumentNullException ( nameof ( transform ) ) ;
55
+ Contract . EndContractBlock ( ) ;
56
+
57
+ ConnectionState state = command . EnsureOpen ( ) ;
58
+ if ( state == ConnectionState . Closed ) behavior |= CommandBehavior . CloseConnection ;
59
+ using SqlDataReader reader = command . ExecuteReader ( behavior ) ;
60
+ return transform ( reader ) ;
61
+ }
62
+
47
63
/// <param name="command">The <see cref="SqlCommand"/> to generate a reader from.</param>
48
64
/// <param name="handler">The handler function for the <see cref="SqlDataReader"/>.</param>
49
65
/// <param name="behavior">The behavior to use with the data reader.</param>
@@ -92,7 +108,7 @@ public static async ValueTask ExecuteReaderAsync(this SqlCommand command,
92
108
}
93
109
94
110
/// <param name="command">The <see cref="DbCommand"/> to generate a reader from.</param>
95
- /// <param name="transform">The transform function for each <see cref="IDataRecord "/>.</param>
111
+ /// <param name="transform">The transform function the <see cref="DbDataReader "/>.</param>
96
112
/// <param name="behavior">The behavior to use with the data reader.</param>
97
113
/// <param name="cancellationToken">The cancellation token.</param>
98
114
/// <inheritdoc cref="CommandExtensions.ExecuteReaderAsync{T}(DbCommand, Func{DbDataReader, ValueTask{T}}, CommandBehavior, CancellationToken)"/>/>
@@ -117,4 +133,27 @@ public static async ValueTask<T> ExecuteReaderAsync<T>(this SqlCommand command,
117
133
using SqlDataReader reader = await command . ExecuteReaderAsync ( behavior , cancellationToken ) . ConfigureAwait ( false ) ;
118
134
return transform ( reader ) ;
119
135
}
136
+
137
+ /// <inheritdoc cref="ExecuteReaderAsync{T}(SqlCommand, Func{SqlDataReader, T}, CommandBehavior, CancellationToken)"/>
138
+ public static async ValueTask < T > ExecuteReaderAsync < T > ( this SqlCommand command ,
139
+ Func < SqlDataReader , ValueTask < T > > transform ,
140
+ CommandBehavior behavior = CommandBehavior . Default ,
141
+ CancellationToken cancellationToken = default )
142
+ {
143
+ if ( command is null ) throw new ArgumentNullException ( nameof ( command ) ) ;
144
+ if ( transform is null ) throw new ArgumentNullException ( nameof ( transform ) ) ;
145
+ Contract . EndContractBlock ( ) ;
146
+
147
+ ConnectionState state = await command
148
+ . EnsureOpenAsync ( cancellationToken )
149
+ . ConfigureAwait ( false ) ;
150
+
151
+ if ( state == ConnectionState . Closed ) behavior |= CommandBehavior . CloseConnection ;
152
+ #if NET472
153
+ #else
154
+ await
155
+ #endif
156
+ using SqlDataReader reader = await command . ExecuteReaderAsync ( behavior , cancellationToken ) . ConfigureAwait ( false ) ;
157
+ return await transform ( reader ) ;
158
+ }
120
159
}
0 commit comments