Exemple #1
0
        internal static int Update <T>(this DbConnection conn, T obj, IQuery <T> where, int?commandTimeout = null) where T : BaseDBModel
        {
            if (obj.GetDBModel_ChangeList().Count == 0)
            {
                throw new Exception("没有修改列");
            }

            var elst       = obj.GetDBModel_PKCols().AddRange(obj.GetDBModel_ExcludeColsForUpdate());
            var updateList = obj.GetDBModel_ChangeList().Where(m => m != obj.GetDBModel_IncrementCol() && !elst.Contains(m)).ToList();

            var sql = new StringBuilder($"UPDATE [{DBTool.GetTableName(obj)}] SET {GetSql(updateList, ",")} WHERE 1=1");
            DynamicParameters dynamicParameters = SetWhereSql(where, sql);

            dynamicParameters.AddDynamicParams(obj);
            try
            {
                return(conn.Execute(sql: sql.ToString(), param: dynamicParameters, commandTimeout: commandTimeout));
            }
            catch (Exception e)
            {
                DBTool.DbError(e, sql.ToString() + JsonHelper.ToJSON(obj) + ToIQueryJSON(dynamicParameters, where) + ExceptionTool.ToString(e));
                throw;
            }
        }
Exemple #2
0
        internal static List <T> GetList <T>(this DbConnection conn, IQuery <T> query, int?commandTimeout = null) where T : BaseDBModel
        {
            var sql = new StringBuilder($"SELECT 0 as DBModel_IsBeginChange, * FROM [{DBTool.GetTableName(query.DBModel)}] WHERE 1=1 ");
            DynamicParameters dynamicParameters = SetWhereSql(query, sql);

            try
            {
                var lst = conn.Query <T>(sql: sql.ToString(), param: dynamicParameters, commandTimeout: commandTimeout).AsList();
                lst.ForEach(m => m.BeginChange());
                return(lst);
            }
            catch (Exception e)
            {
                DBTool.DbError(e, sql.ToString() + ToIQueryJSON(dynamicParameters, query) + ExceptionTool.ToString(e));
                throw;
            }
        }
        public int Execute(T obj, string sql, object par)
        {
            var conn = GetConn(obj);

            return(conn.Execute(sql: sql, param: par, commandTimeout: CommandTimeout, transaction: DBTool.GetTransaction(conn)));
        }
        public R Get <R>(T obj, string sql, object par)
        {
            var conn = GetConn(obj);

            return(conn.QueryFirstOrDefault <R>(sql: sql, param: par, commandTimeout: CommandTimeout, transaction: DBTool.GetTransaction(conn)));
        }
        public Paging <R> GetPaging <R>(ISelectResult <R> query)
        {
            var lst   = GetList(query);
            var conn  = GetConn(null);
            var count = conn.QuerySingleOrDefault <long>(sql: query.CountSqlCmd, param: query.Param, commandTimeout: CommandTimeout, transaction: DBTool.GetTransaction(conn));

            return(new Paging <R>(count, lst));
        }
        public List <R> GetList <R>(ISelectResult <R> query)
        {
            Type            typeFromHandle = typeof(R);
            ConstructorInfo right          = typeFromHandle.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault((ConstructorInfo x) => x.GetParameters().Length == 0);
            var             conn           = GetConn(query.DBModel as T);

            if (null == right && typeFromHandle.FullName.Contains("AnonymousType"))
            {
                using (IDataReader reader = conn.ExecuteReader(sql: query.SqlCmd, param: query.Param, commandTimeout: CommandTimeout, transaction: DBTool.GetTransaction(conn)))
                {
                    return(GetList(reader.Parse <R>().ToList()));
                }
            }

            return(GetList(conn.Query <R>(sql: query.SqlCmd, param: query.Param, commandTimeout: CommandTimeout, transaction: DBTool.GetTransaction(conn))));
        }
        public List <R> GetList <R>(T obj, string sql, object par)
        {
            var conn = GetConn(obj);

            return(GetList(conn.Query <R>(sql: sql, param: par, commandTimeout: CommandTimeout, transaction: DBTool.GetTransaction(conn))));
        }
        public List <T> GetList(string sql, object par)
        {
            var conn = GetConn(null);
            var lst  = conn.Query <T>(sql: sql, param: par, commandTimeout: CommandTimeout, transaction: DBTool.GetTransaction(conn));

            return(GetList(lst));
        }