DALs.Sql/SqlClient.cs
/// <summary>
/// query as an asynchronous operation.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="config">The configuration.</param>
/// <param name="loader">The loader.</param>
/// <returns>Task&lt;T&gt;.</returns>
query as an asynchronous operation.
query as an asynchronous operation .
public virtual async Task<T> QueryAsync<T>(SqlConfiguration config, Func<IDataReader, T> loader = null)
        {
            T result = default(T);
            using (IDbConnection connection = init.DbConnection(config.ConnectionString))
            {
                try
                {
                    using (IDbCommand command = init.DbCommand(config.StoredProcedures.First()))
                    {
                        command.Connection = connection;
                        command.LoadParameters(config.SqlParameters as List<SqlParameter>);
                        connection.Open();
                        switch (config.Mode)
                        {
                            case SprocMode.ExecuteReader:
                                if (loader == null)
                                {
                                    throw new ArgumentNullException("loader", "No specified loader function");
                                }
                                using (var reader = await command.ExecuteReaderAsync())
                                {
                                    result = loader(reader);
                                }
                                break;
                            case SprocMode.ExecuteScalar:
                                result = (T)await command.ExecuteScalarAsync();
                                break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Trace.TraceError(string.Format("{0}: {1}",e.Message, e.StackTrace));
                }
                finally
                {
                    connection.ForceClose();
                }
            }
            return result;
        }
public virtual async Task < T > QueryAsync < T > ( SqlConfiguration config , Func < IDataReader , T > loader = null ) { T result = default ( T ) ; using ( IDbConnection connection = init . DbConnection ( config . ConnectionString ) ) { try { using ( IDbCommand command = init . DbCommand ( config . StoredProcedures . First ( ) ) ) { command . Connection = connection ; command . LoadParameters ( config . SqlParameters as List < SqlParameter > ) ; connection . Open ( ) ; switch ( config . Mode ) { case SprocMode . ExecuteReader : if ( loader == null ) { throw new ArgumentNullException ( " " , " " ) ; } using ( var reader = await command . ExecuteReaderAsync ( ) ) { result = loader ( reader ) ; } break ; case SprocMode . ExecuteScalar : result = ( T ) await command . ExecuteScalarAsync ( ) ; break ; } } } catch ( Exception e ) { Trace . TraceError ( string . Format ( " " , e . Message , e . StackTrace ) ) ; } finally { connection . ForceClose ( ) ; } } return result ; }
query as an asynchronous operation.
query as an asynchronous operation .
None
config
SqlConfiguration
None
loader
Func<IDataReader, T>
None
typeparam
None