Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }