/// <summary> /// Instantiates the object and sets properties based on the field name. Only returns the first row. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="db"></param> /// <param name="cmd"></param> /// <param name="load">A method that will create an object and fill it. If null, the object will be instantiated based on its type using the ClassFactory (must have a default ctor). If this returns null, it will not be added to the results.</param> /// <returns></returns> public async static Task <IEnumerable <T> > GetObjectsAsync <T>(this BaDatabase db, DbCommand cmd, Func <DbDataReader, Task <T> > load = null) where T : class { var results = new List <T>(); // If load doesn't have a value, use the default loader. if (load == null) { var props = TypeDescriptor.GetProperties(typeof(T)); load = async(row) => { var obj = ClassFactory.CreateObject <T>(); FillObject(row, obj, props); return(await Task.FromResult(obj).ConfigureAwait(false)); }; } await db.ExecuteReaderAsync(cmd, async (row) => { var result = await load(row).ConfigureAwait(false); if (result != null) { results.Add(result); } return(true); }).ConfigureAwait(false); return(results); }
/// <summary> /// Gets the first row as a dynamic object. /// </summary> /// <param name="db"></param> /// <param name="cmd"></param> /// <returns></returns> public async static Task <dynamic> GetDynamicAsync(this BaDatabase db, DbCommand cmd) { dynamic result = null; await db.ExecuteReaderAsync(cmd, async (row) => { result = DbDataReaderToDynamic(row); return(await Task.FromResult(false).ConfigureAwait(false)); }).ConfigureAwait(false); return(result); }
/// <summary> /// Returns the results of the SQL command as a list of dynamic objects. /// </summary> /// <param name="db"></param> /// <param name="cmd"></param> /// <returns></returns> public async static Task <IEnumerable <dynamic> > GetDynamicsAsync(this BaDatabase db, DbCommand cmd) { var results = new List <dynamic>(); await db.ExecuteReaderAsync(cmd, async (row) => { var result = DbDataReaderToDynamic(row); results.Add(result); return(await Task.FromResult(true).ConfigureAwait(false)); }).ConfigureAwait(false); return(results); }
/// <summary> /// Instantiates the object and sets properties based on the field name. Only returns the first row. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="db"></param> /// <param name="cmd"></param> /// <param name="load">A method that will create an object and fill it. If null, the object will be instantiated based on its type using the ClassFactory (must have a default ctor).</param> /// <returns></returns> public async static Task <T> GetObjectAsync <T>(this BaDatabase db, DbCommand cmd, Func <IDataReader, Task <T> > load = null) where T : class { T obj = null; await db.ExecuteReaderAsync(cmd, async (row) => { if (load != null) { obj = await load(row).ConfigureAwait(false); return(false); } // Load doesn't have a value, so use the default loader. obj = ClassFactory.CreateObject <T>(); var props = TypeDescriptor.GetProperties(typeof(T)); FillObject(row, obj, props); return(false); }).ConfigureAwait(false); return(obj); }
/// <summary> /// Sends the System.Data.SqlClient.DbCommand.CommandText to the System.Data.SqlClient.DbCommand.Connection /// and builds a System.Data.SqlClient.DbDataReader. The reader is only valid during execution of the method. /// Use processRow to process each row in the reader. /// </summary> /// <param name="db"></param> /// <param name="sprocName">Name of the stored procedure to call.</param> /// <param name="parameters">An object that contains the properties to add as SQL parameters to the SQL command.</param> /// <param name="processRow">Called for each row in the data reader. Return true to continue processing more rows.</param> public static async Task ExecuteReaderAsync(this BaDatabase db, string sprocName, object parameters, Func <DbDataReader, Task <bool> > processRow) { var cmd = db.PrepareSprocCmd(sprocName, parameters); await db.ExecuteReaderAsync(cmd, processRow).ConfigureAwait(false); }