public DbCommandWrapper CreateCommand(CommandWrapperType cmdWrapperType, string query) { if (string.IsNullOrEmpty(query)) { throw new Exception("Sql or stored procedure is empty"); } DbCommandWrapper dbCommandWrapper = new DbCommandWrapper(); switch (cmdWrapperType) { case CommandWrapperType.Text: this.sql = query; this.storedProcName = ""; break; case CommandWrapperType.StoredProcedure: this.storedProcName = query; this.sql = ""; break; default: throw new Exception("ComandWrapperType is null"); } return(dbCommandWrapper); }
public IDataReader ExecuteReader(DbCommandWrapper dbCommandWrapper) { IDataReader rdr = null; try { DbHelper dbHelper = new DbHelper(conn); if (sql.Equals("")) { rdr = dbHelper.ExecuteReader(dbCommandWrapper, true, storedProcName, false, null); } else { rdr = dbHelper.ExecuteReader(dbCommandWrapper, false, sql, false, null); } } catch (Exception ex) { throw ex; } return(rdr); }
public List <T> ExecuteList <T>(DbCommandWrapper dbCommandWrapper, IDataMapper <T> dataMapper, Transaction tx) { List <T> list = null; try { DbHelper dbHelper = new DbHelper(conn); if (sql.Equals("")) { list = dbHelper.ExecuteList <T>(dbCommandWrapper, true, storedProcName, dataMapper, true, tx); } else { list = dbHelper.ExecuteList <T>(dbCommandWrapper, false, sql, dataMapper, true, tx); } } catch (Exception ex) { throw ex; } return(list); }
public List <T> ExecuteList <T>(DbCommandWrapper dbCommandWrapper, IDataMapper <T> dataMapper, int index, int size) { List <T> list = null; try { DbHelper dbHelper = new DbHelper(dataSource); if (sql.Equals("")) { list = dbHelper.ExecuteList <T>(dbCommandWrapper, true, storedProcName, dataMapper, index, size); } else { list = dbHelper.ExecuteList <T>(dbCommandWrapper, false, sql, dataMapper, index, size); } } catch (Exception ex) { throw ex; } return(list); }
public int ExecuteNonQuery(DbCommandWrapper dbCommandWrapper, Transaction tx) { int result = 0; DbHelper dbHelper = null; try { if (tx == null) { throw new ArgumentNullException("tx"); } else { dbHelper = new DbHelper(tx.GetConnection()); } if (sql.Equals("")) { result = dbHelper.ExecuteNonQuery(dbCommandWrapper, true, storedProcName, true, tx); } else { result = dbHelper.ExecuteNonQuery(dbCommandWrapper, false, sql, true, tx); } } catch (Exception ex) { throw ex; } return(result); }
public T ExecuteObject <T>(DbCommandWrapper dbCommandWrapper, IDataMapper <T> dataMapper, Transaction tx) { T obj = default(T); try { DbHelper dbHelper = new DbHelper(conn); if (sql.Equals("")) { obj = dbHelper.ExecuteObject <T>(dbCommandWrapper, true, storedProcName, dataMapper, true, tx); } else { obj = dbHelper.ExecuteObject <T>(dbCommandWrapper, false, sql, dataMapper, true, tx); } } catch (Exception ex) { throw ex; } return(obj); }
private void AddParameters(DbCommand cmd, DbCommandWrapper cmdWrapper) { foreach (ParameterClause param in cmdWrapper.Parameters) { cmd.Parameters.Add(CreateDbParameter(cmd, param)); } }
public int ExecuteNonQuery(DbCommandWrapper dbCommandWrapper) { int result = 0; try { DbHelper dbHelper = new DbHelper(conn); if (sql.Equals("")) { result = dbHelper.ExecuteNonQuery(dbCommandWrapper, true, storedProcName, false, null); } else { result = dbHelper.ExecuteNonQuery(dbCommandWrapper, false, sql, false, null); } } catch (Exception ex) { throw ex; } return(result); }
public object ExecuteScalar(DbCommandWrapper cmdWrapper, bool isStoredProc, string sql, bool isTransaction, Transaction tx) { if (cmdWrapper == null) { throw new Exception("DbCommandWrapper is null"); } if (string.IsNullOrEmpty(sql)) { throw new Exception("Sql or stored procedure is empty"); } object result = null; DbCommand cmd = null; try { cmd = conn.CreateCommand(); cmdWrapper.Command = cmd; if (isTransaction) { cmd.Transaction = tx.GetTransaction(); } if (isStoredProc) { cmd.CommandType = CommandType.StoredProcedure; } else { cmd.CommandType = CommandType.Text; } cmd.CommandText = sql; AddParameters(cmd, cmdWrapper); result = cmd.ExecuteScalar(); cmdWrapper.PopulateOutputParameters(); } catch (Exception ex) { throw ex; } finally { if (cmd != null) { cmd.Dispose(); } } return(result); }
public DataSet ExecuteDataSet(DbCommandWrapper cmdWrapper, bool isStoredProc, string sql) { if (cmdWrapper == null) { throw new Exception("DbCommandWrapper is null"); } if (string.IsNullOrEmpty(sql)) { throw new Exception("Sql or stored procedure is empty"); } DataSet dataSet = null; DbCommand cmd = null; DbDataAdapter da = null; try { DbProviderFactory factory = DbProviderFactories.GetFactory(dataSource.Provider); da = factory.CreateDataAdapter(); cmd.Connection = conn; cmd = conn.CreateCommand(); if (isStoredProc) { cmd.CommandType = CommandType.StoredProcedure; } else { cmd.CommandType = CommandType.Text; } cmd.CommandText = sql; dataSet = new DataSet(); AddParameters(cmd, cmdWrapper); da.SelectCommand = cmd; da.Fill(dataSet); } catch (Exception ex) { throw ex; } finally { if (cmd != null) { cmd.Dispose(); } } return(dataSet); }
public IDataReader ExecuteReader(DbCommandWrapper cmdWrapper, bool isStoredProc, string sql, bool isTransaction, Transaction tx) { if (cmdWrapper == null) { throw new Exception("DbCommandWrapper is null"); } if (string.IsNullOrEmpty(sql)) { throw new Exception("Sql or stored procedure is empty"); } DbCommand cmd = null; IDataReader rdr = null; try { cmd = conn.CreateCommand(); cmdWrapper.Command = cmd; if (isStoredProc) { cmd.CommandType = CommandType.StoredProcedure; } else { cmd.CommandType = CommandType.Text; } cmd.CommandText = sql; if (isTransaction) { cmd.Transaction = tx.GetTransaction(); } AddParameters(cmd, cmdWrapper); rdr = cmd.ExecuteReader(); } catch (Exception ex) { throw ex; } finally { if (cmd != null) { cmd.Dispose(); } } return(rdr); }
public object ExecuteScalar(DbCommandWrapper dbCommandWrapper) { object result = null; try { DbHelper dbHelper = new DbHelper(conn); if (sql.Equals("")) { result = dbHelper.ExecuteScalar(dbCommandWrapper, true, storedProcName, false, null); } else { result = dbHelper.ExecuteScalar(dbCommandWrapper, false, sql, false, null); } } catch (Exception ex) { throw ex; } return(result); }
public DataSet ExecuteDataSet(DbCommandWrapper dbCommandWrapper) { DataSet dataSet = null; try { DbHelper dbHelper = new DbHelper(conn); if (sql.Equals("")) { dataSet = dbHelper.ExecuteDataSet(dbCommandWrapper, true, storedProcName); } else { dataSet = dbHelper.ExecuteDataSet(dbCommandWrapper, false, sql); } } catch (Exception ex) { throw ex; } return(dataSet); }
public List <T> ExecuteList <T>(DbCommandWrapper cmdWrapper, bool isStoredProc, string sql, IDataMapper <T> dataMapper, int index, int size) { if (cmdWrapper == null) { throw new Exception("DbCommandWrapper is null"); } if (string.IsNullOrEmpty(sql)) { throw new Exception("Sql or stored procedure is empty"); } if (dataMapper == null) { throw new Exception("Mapper is null"); } List <T> list = new List <T>(); IDataReader rdr = null; DataSet dataSet = null; DbCommand cmd = null; DbDataAdapter da = null; try { DbProviderFactory factory = DbProviderFactories.GetFactory(dataSource.Provider); da = factory.CreateDataAdapter(); cmd = conn.CreateCommand(); cmdWrapper.Command = cmd; cmd.Connection = conn; if (isStoredProc) { cmd.CommandType = CommandType.StoredProcedure; } else { cmd.CommandType = CommandType.Text; } cmd.CommandText = sql; dataSet = new DataSet(); da.SelectCommand = cmd; da.Fill(dataSet, index, size, typeof(T).ToString()); AddParameters(cmd, cmdWrapper); rdr = dataSet.Tables[typeof(T).ToString()].CreateDataReader(); while (rdr.Read()) { list.Add(dataMapper.Map(rdr)); } cmdWrapper.PopulateOutputParameters(); } catch (Exception ex) { throw ex; } finally { if (cmd != null) { cmd.Dispose(); } if (da != null) { da.Dispose(); } if (dataSet != null) { dataSet.Dispose(); } if (rdr != null) { rdr.Close(); } } return(list); }
public List <T> ExecuteList <T>(DbCommandWrapper cmdWrapper, bool isStoredProc, string sql, IDataMapper <T> dataMapper, bool isTransaction, Transaction tx) { if (cmdWrapper == null) { throw new Exception("DbCommandWrapper is null"); } if (string.IsNullOrEmpty(sql)) { throw new Exception("Sql or stored procedure is empty"); } if (dataMapper == null) { throw new Exception("Mapper is null"); } List <T> list = new List <T>(); DbCommand cmd = null; IDataReader rdr = null; try { cmd = conn.CreateCommand(); cmdWrapper.Command = cmd; if (isTransaction) { cmd.Transaction = tx.GetTransaction(); } if (isStoredProc) { cmd.CommandType = CommandType.StoredProcedure; } else { cmd.CommandType = CommandType.Text; } cmd.CommandText = sql; AddParameters(cmd, cmdWrapper); if (rdr != null) { rdr.Close(); } rdr = cmd.ExecuteReader(); while (rdr.Read()) { list.Add(dataMapper.Map(rdr)); } cmdWrapper.PopulateOutputParameters(); } catch (Exception ex) { throw ex; } finally { if (cmd != null) { cmd.Dispose(); } if (rdr != null) { rdr.Close(); } } return(list); }