Пример #1
0
 public void DeleteBy(string field, object value)
 {
     using (var cmd = new SqlCommand("DELETE FROM " + table + " WHERE " + field + "=@value", Conn.CreateConnection()))
     {
         cmd.Parameters.AddWithValue("@value", value);
         cmd.ExecuteNonQuery();
         cmd.Connection.Close();
     }
 }
Пример #2
0
        public void Insert(T pro)
        {
            string parms   = "";
            string fielsds = "";

            var mappings = mapper.CreateMap();

            foreach (var map in mappings)
            {
                if (map.Key.ToLower() != "id")
                {
                    fielsds += map.Value + ", ";
                    parms   += "@" + map.Key + ", ";
                }
            }

            fielsds = fielsds.Substring(0, fielsds.Length - 2);
            parms   = parms.Substring(0, parms.Length - 2);

            using (var cmd = new SqlCommand("INSERT INTO " + table + " (" + fielsds + ") VALUES(" + parms + ")", Conn.CreateConnection()))
            {
                foreach (var prop in mappings)
                {
                    if (prop.Key.ToLower() != "id")
                    {
                        cmd.Parameters.AddWithValue(prop.Key, pro.GetType().GetProperty(prop.Key).GetValue(pro, null));
                    }
                }

                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
        }
Пример #3
0
 public void Delete(int ID)
 {
     using (var cmd = new SqlCommand("DELETE FROM " + table + " WHERE ID=@ID", Conn.CreateConnection()))
     {
         cmd.Parameters.AddWithValue("ID", ID);
         cmd.ExecuteNonQuery();
         cmd.Connection.Close();
     }
 }
Пример #4
0
        public List <T> GetBy(string field, object value)
        {
            using (var cmd = new SqlCommand("SELECT * FROM " + table + " WHERE " + field + "=@KID", Conn.CreateConnection()))
            {
                cmd.Parameters.AddWithValue("@KID", value);

                List <T> list = mapper.MapList(cmd.ExecuteReader());
                cmd.Connection.Close();
                return(list);
            }
        }
Пример #5
0
        public T Get(int ID)
        {
            using (var cmd = new SqlCommand("SELECT * FROM " + table + " WHERE ID=@ID", Conn.CreateConnection()))
            {
                cmd.Parameters.AddWithValue("@ID", ID);

                var r    = cmd.ExecuteReader();
                T   type = new T();

                if (r.Read())
                {
                    type = mapper.Map(r);
                }

                r.Close();
                cmd.Connection.Close();
                return(type);
            }
        }
Пример #6
0
        public void UpdateField(string field, object value, int ID)
        {
            using (var cmd = new SqlCommand("UPDATE " + table + " SET " + field + "=@value WHERE ID=@Id", Conn.CreateConnection()))
            {
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.Parameters.AddWithValue("@value", value);

                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
        }
Пример #7
0
        public void Update(T pro)
        {
            string fAndP    = "";
            var    mappings = mapper.CreateMap();

            foreach (var map in mappings)
            {
                if (map.Key.ToLower() != "id")
                {
                    fAndP += map.Value + "=@" + map.Key + ", ";
                }
            }

            fAndP = fAndP.Substring(0, fAndP.Length - 2);

            using (var cmd = new SqlCommand("UPDATE " + table + " SET " + fAndP + " WHERE ID=@Id", Conn.CreateConnection()))
            {
                foreach (var prop in mappings)
                {
                    cmd.Parameters.AddWithValue(prop.Key, pro.GetType().GetProperty(prop.Key).GetValue(pro, null));
                }

                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
        }