/// <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 static IEnumerable <T> GetObjects <T>(this BaDatabase db, DbCommand cmd, Func <DbDataReader, 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 = (row) => { var obj = ClassFactory.CreateObject <T>(); FillObject(row, obj, props); return(obj); }; } db.ExecuteReader(cmd, (row) => { var result = load(row); if (result != null) { results.Add(result); } return(true); }); return(results); }
/// <summary> /// Gets the first row as a dynamic object. /// </summary> /// <param name="db"></param> /// <param name="cmd"></param> /// <returns></returns> public static dynamic GetDynamic(this BaDatabase db, DbCommand cmd) { dynamic result = null; db.ExecuteReader(cmd, (row) => { result = DbDataReaderToDynamic(row); return(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 static IEnumerable <dynamic> GetDynamics(this BaDatabase db, DbCommand cmd) { var results = new List <dynamic>(); db.ExecuteReader(cmd, (row) => { var result = DbDataReaderToDynamic(row); results.Add(result); return(true); }); 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 static T GetObject <T>(this BaDatabase db, DbCommand cmd, Func <IDataReader, T> load = null) where T : class { T obj = null; db.ExecuteReader(cmd, (row) => { if (load != null) { obj = load(row); 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); }); 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 void ExecuteReader(this BaDatabase db, string sprocName, object parameters, Func <DbDataReader, bool> processRow) { var cmd = db.PrepareSprocCmd(sprocName, parameters); db.ExecuteReader(cmd, processRow); }