Пример #1
0
        public void Update(T obj)
        {
            string tableName = Reflections.GetTableName(typeof(T));
            string pk        = Reflections.GetPk(typeof(T));

            string expression = String.Empty;

            var dict = obj.GetParamsDict <S>();

            var pkWithValue = obj.GetPk <S>();


            foreach (var item in dict)
            {
                expression += String.Format("{0}=@{0},", item.Key);
            }

            dict.Add(pkWithValue.First().Key, pkWithValue.First().Value);
            expression = expression.Substring(0, expression.Length - 1);


            //string query = $"Update {tableName} set {expression} where {pk} = @{pk}";

            string query = String.Format("Update {0} set {1} where {2} = @{2}", tableName, expression, pk);

            using (var db = new SqlConnection(_connectionString))
            {
                db.Execute(query, dict);
            }
        }
Пример #2
0
        public int Insert(T obj)
        {
            string tableName = Reflections.GetTableName(typeof(T));

            string colNames = String.Empty;
            string parNames = String.Empty;

            bool withPk = !obj.Id.Equals(default(S));

            var dict = obj.GetParamsDict <S>(withPk);

            foreach (var item in dict)
            {
                colNames += String.Format("{0},", item.Key);
                parNames += String.Format("@{0},", item.Key);
            }

            colNames = colNames.Substring(0, colNames.Length - 1);
            parNames = parNames.Substring(0, parNames.Length - 1);

            string query = String.Format("Insert into {0}({1}) values({2})", tableName, colNames, parNames);

            using (var db = new SqlConnection(_connectionString))
            {
                return(db.Execute(query, dict));
            }
        }
Пример #3
0
 public void Delete(S objId)
 {
     using (var db = new SqlConnection(_connectionString))
     {
         db.Execute(String.Format("Delete From {0} where {1} = @id", Reflections.GetTableName(typeof(T)), Reflections.GetPk(typeof(T))), new { id = objId });
     }
 }
Пример #4
0
        public static string GetQuery(Type type)
        {
            string tableName = Reflections.GetTableName(type);

            var properties = type.GetProperties();

            string expression = "Select ";

            foreach (var prop in properties)
            {
                var attrs = prop.GetCustomAttributes(true).Where(o => o is DalObjAttribute);

                foreach (var dalAttr in attrs)
                {
                    DalObjAttribute custDalAttr = dalAttr as DalObjAttribute;

                    expression += String.Format("{0}.{1} as {2},", tableName, custDalAttr.Alias, prop.Name);
                }
            }

            expression = expression.Substring(0, expression.Length - 1);

            expression += String.Format(" From {0}", tableName);

            return(expression);
        }
Пример #5
0
 public T GetById(S id)
 {
     using (var db = new SqlConnection(_connectionString))
     {
         var data = db.Query <T>(String.Format("{0} where {1} = @id", Reflections.GetQuery(typeof(T)), Reflections.GetPk(typeof(T))), new  { id = id });
         return(data.SingleOrDefault());
     }
 }
Пример #6
0
        public IEnumerable <T> GetAll()
        {
            using (var db = new SqlConnection(_connectionString))
            {
                var data = db.Query <T>(Reflections.GetQuery(typeof(T)));

                return(data);
            }
        }