/// <summary> /// Executes a database query asynchronously. /// </summary> /// <param name="callback">The callback method.</param> /// <param name="userState">The user state.</param> /// <returns>The asynchronous result.</returns> public override IAsyncResult ExecuteReader(DbCommandReaderCallback callback, object userState = null) { // Create the asynchronous result. DbAsyncResult asyncResult = new DbAsyncResult(userState); // Begin execute the command asynchronously. this.command.BeginExecuteReader((IAsyncResult result) => { // Create a new reader for this command. DbReaderSql reader = null; // Set the synchronous completion of the command. asyncResult.CompletedSynchronously = result.CompletedSynchronously; try { // Try and execute the command. SqlDataReader sqlReader = this.command.EndExecuteReader(result); // Create a new reader instance. reader = new DbReaderSql(sqlReader); } catch (SqlException exception) { // If an exception occurs, set the exception. asyncResult.Exception = new DbException("The execution of the SQL query \'{0}\' failed. {1}".FormatWith(this.Query, exception.Message), exception); } catch (Exception exception) { // If an exception occurs, set the exception. asyncResult.Exception = new DbException("The execution of the SQL query \'{0}\' failed.".FormatWith(this.Query), exception); } // Complete the asynchronous operation. asyncResult.Complete(); // Call the callback method. if (callback != null) callback(asyncResult, reader); }, asyncResult.AsyncState); return asyncResult; }
/// <summary> /// Executes a database statement asynchronously. /// </summary> /// <param name="callback">The callback method.</param> /// <param name="userState">The user state.</param> /// <returns>The asynchronous result.</returns> public override IAsyncResult ExecuteNonQuery(DbCommandNonQueryCallback callback, object userState = null) { // Create the asynchronous result. DbAsyncResult asyncResult = new DbAsyncResult(userState); // Begin execute the command asynchronously. this.command.BeginExecuteNonQuery((IAsyncResult result) => { // Set the synchronous completion of the command. asyncResult.CompletedSynchronously = result.CompletedSynchronously; int count = 0; try { count = this.command.EndExecuteNonQuery(result); } catch (Exception exception) { // If an exception occurs, set the exception. asyncResult.Exception = new DbException("The execution of the SQL statement \'{0}\' failed.".FormatWith(this.Query), exception); } // Complete the asynchronous operation. asyncResult.Complete(); // Call the callback method. if (callback != null) callback(asyncResult, count); }, asyncResult.AsyncState); return asyncResult; }