/// <summary> /// Execute query on the database without the regards for result. /// Connection and transaction information will be appended to the provided command. /// </summary> /// <param name="query">ADO.NET driver</param> /// <param name="sql">sql to execute</param> /// <param name="parameters">sql parameters</param> /// <returns>comand.Execute() result</returns> public static int Execute( this IDatabaseQuery query, string sql, params object[] parameters) { Contract.Requires(query != null); Contract.Requires(!string.IsNullOrWhiteSpace(sql)); var com = query.CreateCommand(sql, parameters); return(query.Execute(com)); }
/// <summary> /// Execute query on the database and loop through the reader. /// Connection and transaction information will be appended to the provided command. /// Provided parameters will be inserted into the command. /// command.ExecuteDataReader() will be called. /// </summary> /// <param name="query">ADO.NET driver</param> /// <param name="sql">sql to execute</param> /// <param name="action">datareader converter</param> /// <param name="parameters">command parameters</param> public static void Execute( this IDatabaseQuery query, string sql, Action <IDataReader> action, params object[] parameters) { Contract.Requires(query != null); Contract.Requires(!string.IsNullOrWhiteSpace(sql)); var com = query.CreateCommand(sql, parameters); query.Execute(com, action); }
/// <summary> /// Execute query on the database and fill DataTable with the result. /// Connection and transaction information will be appended to the provided command. /// </summary> /// <param name="query">ADO.NET driver</param> /// <param name="sql">sql to execute</param> /// <param name="parameters">sql additional parameters</param> /// <returns>populated table</returns> public static DataTable Fill( this IDatabaseQuery query, string sql, params object[] parameters) { Contract.Requires(query != null); Contract.Requires(!string.IsNullOrWhiteSpace(sql)); var com = query.CreateCommand(sql, parameters); var ds = new DataSet(); query.Fill(com, ds); return(ds.Tables.Count > 0 ? ds.Tables[0] : new DataTable()); }
/// <summary> /// Execute query on the database and return converted result. /// Connection and transaction information will be appended to the provided command. /// </summary> /// <typeparam name="T">result type</typeparam> /// <param name="query">ADO.NET driver</param> /// <param name="sql">sql to execute</param> /// <param name="instancer">object factory</param> /// <param name="parameters"></param> /// <returns></returns> public static T Get <T>( this IDatabaseQuery query, string sql, Func <IDataReader, T> instancer, params object[] parameters) { Contract.Requires(query != null); Contract.Requires(!string.IsNullOrWhiteSpace(sql)); var com = query.CreateCommand(sql, parameters); T t = default(T); query.Execute(com, dr => t = instancer(dr)); return(t); }
/// <summary> /// Execute query on the database and loop through the reader. /// Return list populated from instance factory. /// Connection and transaction information will be appended to the provided command. /// command.ExecuteDataReader() will be called. /// </summary> /// <typeparam name="T">result type</typeparam> /// <param name="query">ADO.NET driver</param> /// <param name="sql">sql to execute</param> /// <param name="instancer">object factory</param> /// <param name="parameters">additional command parameters</param> /// <returns>populated list</returns> public static List <T> Fill <T>( this IDatabaseQuery query, string sql, Func <IDataReader, T> instancer, params object[] parameters) { Contract.Requires(query != null); Contract.Requires(!string.IsNullOrWhiteSpace(sql)); var com = query.CreateCommand(sql, parameters); var list = new List <T>(); query.Execute(com, dr => list.Add(instancer(dr))); return(list); }