/// <summary> /// Processes a SqlDataReader calling the processRow delegate for each row. /// </summary> /// <param name="cmd"></param> /// <param name="processRow">void ProcessDataRow(SqlDataReader dr)</param> public void ExecuteDataReader(SqlCommand cmd, ProcessRowDelegate processRow) { ExecuteCommand(cmd, () => { var dr = cmd.ExecuteReader(); while (dr.Read()) { processRow(dr); } }); }
/// <summary> /// Executes a command and returns the first row as the given object. The class must have a default constructor. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cmd"></param> /// <param name="process">If not set, sets properties based on the field name returned from the command.</param> /// <returns></returns> public T GetObject <T>(SqlCommand cmd, ProcessRowDelegate <T> process = null) where T : class { if (process == null) { process = new ProcessRowDelegate <T>(ProcessRow <T>); } T obj = null; ExecuteDataReader(cmd, (row) => { obj = process(row); return(false); }); return(obj); }
/** Reads the table's rows. */ public void Read(ProcessRowDelegate processRowDelegate) { while (true) { string[] columns = ReadNextRow(); if (columns != null) { processRowDelegate(columns); } else { break; } } ; }
/// <summary> /// Executes a command and returns the rows as an array of objects. The class must have a default constructor. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cmd"></param> /// <param name="process">If not set, sets properties based on the field name returned from the command.</param> /// <returns></returns> public T[] GetObjects <T>(SqlCommand cmd, ProcessRowDelegate <T> process = null) where T : class { if (process == null) { process = new ProcessRowDelegate <T>(ProcessRow <T>); } var objs = new List <T>(); ExecuteDataReader(cmd, (row) => { var obj = process(row); if (obj != null) { objs.Add(obj); } return(true); }); return(objs.ToArray()); }