Ejemplo n.º 1
0
        public static int Execute(this DBConnectionWrapper conn, Sql sql, bool storeProcedure = false, DBTransactionWrapper trans = null)
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            if (storeProcedure)
            {
                command.CommandType = CommandType.StoredProcedure;
            }

            int ret = 0;

            try
            {
                ret = command.ExecuteNonQuery();
                for (int i = 0; i < sql.Parameters.Count; i++)
                {
                    var p = sql.Parameters[i];
                    if (p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Output || p.Direction == ParameterDirection.ReturnValue)
                    {
                        var val = ((DbParameter)command.Parameters[p.Name]).Value;
                        p.Output = (val == DBNull.Value) ? null : val;
                    }
                }
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }

            return(ret);
        }
Ejemplo n.º 2
0
        public static T GetSingleValue <T>(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null)
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            try
            {
                object val = command.ExecuteScalar();
                return(ParseFromDBValue <T>(val));
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }
        }
Ejemplo n.º 3
0
        public static List <T> Query <T>(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null) where T : new()
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            try
            {
                using (IDataReader reader = command.ExecuteReader())
                {
                    return(reader.ToList <T>());
                }
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }
        }
Ejemplo n.º 4
0
        public static List <T> GetFirstColumn <T>(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null)
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            var mapping = DBObjectMapping <SingleColumn <T> > .Get().ColumnMappingList.Single();

            List <T> ret = new List <T>();

            try
            {
                using (IDataReader reader = command.ExecuteReader())
                {
                    SingleColumn <T> obj = new SingleColumn <T>();

                    Action <SingleColumn <T>, IDataRecord, int> setter = null;
                    if (reader.Read())
                    {
                        setter = mapping.GetPropSetterForRecord(reader.GetFieldType(0));
                        reader.SetProperty(obj, mapping, setter, 0);
                        ret.Add(obj.Val);
                    }

                    while (reader.Read())
                    {
                        reader.SetProperty(obj, mapping, setter, 0);
                        ret.Add(obj.Val);
                    }
                }
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }
            return(ret);
        }