private void EndExecuteReaderCallBack(IAsyncResult result) { DbAsyncState state = null; try { // Retrieve the original command object, passed // to this procedure in the AsyncState property // of the IAsyncResult parameter. DaabAsyncResult blockResult = (DaabAsyncResult)result; state = (DbAsyncState)blockResult.AsyncState; state.State = (object)state.Database.EndExecuteReader(blockResult); state.AsyncResult = blockResult; } catch (Exception exp) { state.Exception = exp; } finally { if (state != null) { state.AutoResetEvent.Set(); } } }
/// <summary> /// Finishes asynchronous execution of a Transact-SQL statement, returning an <see cref="IDataReader"/>. /// </summary> /// <param name="asyncResult"> /// <para>The <see cref="IAsyncResult"/> returned by a call to any overload of BeginExecuteReader.</para> /// </param> /// <seealso cref="Database.ExecuteReader(DbCommand)"/> /// <seealso cref="BeginExecuteReader(DbCommand,AsyncCallback,object)"/> /// <seealso cref="BeginExecuteReader(DbCommand, DbTransaction,AsyncCallback,object)"/> /// <returns> /// <para>An <see cref="IDataReader"/> object that can be used to consume the queried information.</para> /// </returns> public override IDataReader EndExecuteReader(IAsyncResult asyncResult) { DaabAsyncResult daabAsyncResult = (DaabAsyncResult)asyncResult; SqlCommand command = (SqlCommand)daabAsyncResult.Command; try { IDataReader reader = command.EndExecuteReader(daabAsyncResult.InnerAsyncResult); return(reader); } catch (Exception) { if (command.Transaction == null) { // for a reader, the standard cleanup will not close the connection, so it needs to be closed // in the catch block if necessary command.Connection.Close(); } throw; } finally { CleanupConnectionFromAsyncOperation(daabAsyncResult); } }
public override IDataReader EndExecuteReader(IAsyncResult asyncResult) { DaabAsyncResult daabAsyncResult = (DaabAsyncResult)asyncResult; SqlCommand command = (SqlCommand)daabAsyncResult.Command; try { IDataReader reader = command.EndExecuteReader(daabAsyncResult.InnerAsyncResult); //instrumentationProvider.FireCommandExecutedEvent(daabAsyncResult.StartTime); return(reader); } catch (Exception ex) { //instrumentationProvider.FireCommandFailedEvent(command.CommandText, ConnectionStringNoCredentials, e); if (command.Transaction == null) { // for a reader, the standard cleanup will not close the connection, so it needs to be closed // in the catch block if necessary command.Connection.Close(); } throw ex; } finally { CleanupConnectionFromAsyncOperation(daabAsyncResult); } }
/// <summary> /// Finishes asynchronous execution of a Transact-SQL statement, returning the number of affected records. /// </summary> /// <param name="asyncResult"> /// <para>The <see cref="IAsyncResult"/> returned by a call to any overload of <see cref="BeginExecuteNonQuery(DbCommand, AsyncCallback, object)"/>.</para> /// </param> /// <seealso cref="Database.ExecuteNonQuery(DbCommand)"/> /// <seealso cref="BeginExecuteNonQuery(DbCommand, AsyncCallback, object)"/> /// <seealso cref="BeginExecuteNonQuery(DbCommand, DbTransaction, AsyncCallback, object)"/> /// <returns> /// <para>The number of affected records.</para> /// </returns> public override int EndExecuteNonQuery(IAsyncResult asyncResult) { DaabAsyncResult daabAsyncResult = (DaabAsyncResult)asyncResult; SqlCommand command = (SqlCommand)daabAsyncResult.Command; try { int affected = command.EndExecuteNonQuery(daabAsyncResult.InnerAsyncResult); return(affected); } finally { CleanupConnectionFromAsyncOperation(daabAsyncResult); } }
private static void CleanupConnectionFromAsyncOperation(DaabAsyncResult daabAsyncResult) { if (daabAsyncResult.DisposeCommand) { if (daabAsyncResult.Command != null) { daabAsyncResult.Command.Dispose(); } } if (daabAsyncResult.CloseConnection) { if (daabAsyncResult.Connection != null) { daabAsyncResult.Connection.Close(); } } }
public void EndExecuteAccessor <T>(IAsyncResult result) { DaabAsyncResult daabResult = (DaabAsyncResult)result; DbAsyncState state = (DbAsyncState)daabResult.AsyncState; try { DataAccessor <T> accessor = (DataAccessor <T>)state.Accessor; state.State = accessor.EndExecute(result); } catch (Exception e) { state.Exception = e; } finally { state.AutoResetEvent.Set(); } }