Ejemplo n.º 1
0
        public static int Add <T>(T t)
        {
            //String sql = $"insert into {typeof(T).Name} values";
            //for(int i = 0; i < typeof(T).GetProperties().Length ;i++)
            //{
            //    sql+=($" '{typeof(T).GetProperties()[i].GetValue(t,null)}',");
            //}

            //sql.Remove(sql.Length-1);
            //sql += ")";
            //Sqlhelp.AnModel(sql);
            String sql = $"insert into {typeof(T).Name} values(";

            foreach (var a in typeof(T).GetProperties())
            {
                sql += $"@{a.Name},";
            }

            sql.Remove(sql.Length - 1);
            sql += ")";
            SqlParameter[] param = new SqlParameter[typeof(T).GetProperties().Length - 1];
            for (int i = 0; i < typeof(T).GetProperties().Length; i++)
            {
                param[i] = new SqlParameter($"{typeof(T).GetProperties()[i].Name}",
                                            typeof(T).GetProperties()[i].GetValue(t, null));
            }
            return(Sqlhelp.AnModel(sql, param));
        }
Ejemplo n.º 2
0
        public static void Update <T>(int id, T t)
        {
            //UPDATE   Customers SET cust_email = ' [email protected]' WHERE cust_id = '10000005';
            String sql = $"update {typeof(T).Name} set ";

            foreach (var item in typeof(T).GetProperties())
            {
                sql += $"{item.Name}={item.GetValue(t, null)},";
            }

            sql.Remove(sql.Length - 1);
            sql += "where id=" + id + "";
            Sqlhelp.AnModel(sql);
        }
Ejemplo n.º 3
0
        public static int Del <T>(int id)
        {
            ////delete from testss.dbo.test1 where id='12';
            //String sql = $"delete from {typeof(T).Name} where";
            //for (int i = 0; i < typeof(T).GetProperties().Length; i++)
            //{
            //    if (typeof(T).GetProperties()[i].GetValue(t, null) != null)
            //        sql += ($"{typeof(T).GetProperties()[i].Name} = '{typeof(T).GetProperties()[i].GetValue(t, null)}',");
            //    continue;
            //}
            //sql.Remove(sql.Length - 1);
            //sql += ")";
            //Sqlhelp.AnModel(sql);
            string sql = $"delete from {typeof(T).Name} where {typeof(T).GetProperties()[0].Name}=@{typeof(T).GetProperties()[0].Name}";

            SqlParameter[] param =
            {
                new SqlParameter($"@{typeof(T).GetProperties()[0].Name}", id)
            };
            return(Sqlhelp.AnModel(sql, param));
        }