Method ExecuteReader
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.MSSqlClient.dll
ExecuteReader(SqlCommand, Action<SqlDataReader>, CommandBehavior)
Executes the command and passes the SqlDataReader to the handler. The reader is disposed afterwards.
public static void ExecuteReader(this SqlCommand command, Action<SqlDataReader> handler, CommandBehavior behavior = CommandBehavior.Default)
Parameters
commandSqlCommandThe command to execute. Its connection is opened if needed and closed again if it was opened here; neither the command nor the connection is disposed.
handlerAction<SqlDataReader>The function that reads from the reader. Do not let the reader escape it.
behaviorCommandBehaviorThe behavior to use with the data reader. CloseConnection is added when the command opens a closed connection.
Remarks
Exceptions from the provider (for example SqlException) propagate unchanged. The reader is disposed, and with it a connection that this method opened, even when the handler throws.
Exceptions
- ArgumentNullException
commandorhandleris null.
ExecuteReader<T>(SqlCommand, Func<SqlDataReader, T>, CommandBehavior)
Executes the command and passes the SqlDataReader to the transform function, returning its result. The reader is disposed afterwards.
public static T ExecuteReader<T>(this SqlCommand command, Func<SqlDataReader, T> transform, CommandBehavior behavior = CommandBehavior.Default)
Parameters
commandSqlCommandThe command to execute. Its connection is opened if needed and closed again if it was opened here; neither the command nor the connection is disposed.
transformFunc<SqlDataReader, T>The function that reads from the reader and produces the result. Do not let the reader escape it.
behaviorCommandBehaviorThe behavior to use with the data reader. CloseConnection is added when the command opens a closed connection.
Returns
- T
The value returned by
transform.
Type Parameters
TThe type returned by the transform function.
Remarks
Exceptions from the provider (for example SqlException) propagate unchanged. The reader is disposed, and with it a connection that this method opened, even when the transform function throws.
Exceptions
- ArgumentNullException
commandortransformis null.