示例#1
0
        public int Update <T>(object obj, MySearchUtil util)
        {
            using (var conn = GetConnection())
            {
                string where = util.GetConditionClaus();
                var param = util.GetParameters();

                var props  = obj.GetType().GetProperties();
                var entity = MyContainer.Get(typeof(T));

                StringBuilder sb    = new StringBuilder();
                List <string> _cols = new List <string>();

                List <string> propNames = new List <string>();
                foreach (var p in props)
                {
                    propNames.Add(p.Name);
                    param.Add(p.Name, p.GetValue(obj));
                    _cols.Add($"{p.Name}=@{p.Name}");
                }

                sb.Append($"UPDATE [{entity.TableName}] SET ");
                sb.Append(string.Join(',', _cols));
                sb.Append($" WHERE {where}");

                return(conn.Execute(sb.ToString(), obj));
            }
        }
示例#2
0
 /// <summary>
 /// 加载实体
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="util"></param>
 /// <param name="cols"></param>
 /// <returns></returns>
 public T Load <T>(MySearchUtil util, string cols = "*")
 {
     using (var conn = GetConnection())
     {
         string sql = "SELECT TOP 1 " + cols + " FROM [" + GetTableName(typeof(T)) + "] WHERE " +
                      util.GetConditionClaus() + (string.IsNullOrWhiteSpace(util.GetOrderByClaus()) ? "" : " ORDER BY " + util.GetOrderByClaus());
         return(conn.Query <T>(sql, util.GetParameters()).SingleOrDefault());
     }
 }
示例#3
0
        /// <summary>
        /// 获取指定表的所有数据,返回dynamic类型的数据列表
        /// </summary>
        /// <param name="util">查询参数</param>
        /// <param name="table">要查询的表</param>
        /// <param name="cols">要查询的列</param>
        /// <param name="top">指定数量</param>
        /// <returns></returns>
        public IEnumerable <dynamic> Fetch(MySearchUtil util, string table, string cols = "*", int?top = null)
        {
            string where = util.GetConditionClaus();
            string            orderby = util.GetOrderByClaus();
            DynamicParameters param   = util.GetParameters();

            string sql = GetQuerySql(cols, table, where, orderby);

            using (var conn = GetConnection())
            {
                return(conn.Query(sql, param));
            }
        }
示例#4
0
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="util">查询参数</param>
        /// <param name="table">查询的数据表</param>
        /// <param name="cols">要查询的列</param>
        /// <param name="top">要获取的数量</param>
        /// <returns></returns>
        public IEnumerable <T> Fetch <T>(MySearchUtil util, string table = "", string cols = "*", int?top = null)
        {
            if (string.IsNullOrWhiteSpace(table))
            {
                table = GetTableName(typeof(T));
            }

            string where = util.GetConditionClaus();
            string            orderby = util.GetOrderByClaus();
            DynamicParameters param   = util.GetParameters();

            string sql = GetQuerySql(cols, table, where, orderby, top);

            using (var conn = GetConnection())
            {
                return(conn.Query <T>(sql, param));
            }
        }
示例#5
0
 public int Update(KeyValuePairs cols, string table, MySearchUtil util)
 {
     using (var conn = GetConnection())
     {
         var           _params      = util.GetParameters();
         List <string> _update_cols = new List <string>();
         int           idx          = 0;
         string        _paramName   = "";
         foreach (var kv in cols)
         {
             _paramName = _paramPrefix + "c" + idx.ToString();
             _update_cols.Add(kv.Key + "=" + _paramName);
             _params.Add(_paramName, kv.Value);
             idx++;
         }
         string sql = "UPDATE [" + table + "] SET " + string.Join(",", _update_cols) + " WHERE " + util.GetConditionClaus();
         return(conn.Execute(sql, _params));
     }
 }
示例#6
0
        public int GetCount(string tableName, MySearchUtil util = null)
        {
            string            condition = "";
            DynamicParameters param     = null;

            if (util != null)
            {
                condition = util.GetConditionClaus();
                param     = util.GetParameters();
            }
            else
            {
                condition = "1=1";
            }

            using (var conn = GetConnection())
            {
                string sql = "SELECT COUNT(0) FROM [" + tableName + "] WHERE " + condition;
                return(conn.ExecuteScalar <int>(sql, param));
            }
        }