示例#1
0
        public bool Delete(int id)
        {
            string sql   = $"Delete from {typeof(T).Name} where Id={id}";
            int    count = SQLServerProject.ExecuteNonQuery(sql);

            return(count > 0);
        }
示例#2
0
        public void Update(T model)
        {
            PropertyInfo[]      pis = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <SqlParameter> lst = new List <SqlParameter>();
            string set = string.Empty;

            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name == "Id")
                {
                    continue;
                }
                string name  = pi.Name;
                object value = pi.GetValue(model);
                if (!string.IsNullOrEmpty(set))
                {
                    set += " ,";
                }
                set += $"{name}=@{name}";
                SqlParameter par = new SqlParameter($"@{name}", value);
                lst.Add(par);
            }
            string sql = $"Update {typeof(T).Name} Set {set} Where Id={model.Id};";

            SQLServerProject.ExecuteNonQuery(sql, lst);
        }
示例#3
0
        public bool Add(T model)
        {
            PropertyInfo[]      pis = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <SqlParameter> lst = new List <SqlParameter>();
            string sql     = $"Insert Into {typeof(T).Name}";
            string columns = string.Empty;
            string values  = string.Empty;

            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name == "Id")
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(columns))
                {
                    columns += " ,";
                }
                if (!string.IsNullOrEmpty(values))
                {
                    values += " ,";
                }
                string name  = pi.Name;
                object value = pi.GetValue(model);
                columns += name;
                values  += $"@{name}";
                SqlParameter par = new SqlParameter($"@{name}", value);
                lst.Add(par);
            }
            sql = $"{sql}({columns}) Values({values})";
            int count = SQLServerProject.ExecuteNonQuery(sql, lst);

            return(count > 0);
        }