コード例 #1
0
        public static PagedResult <T> PagedQuery <T>(this IDbConnection db, int pageSize, int pageNumber,
                                                     Expression <Func <T, bool> > whereExpression = null, Expression <Func <T, object> > groupByexpression = null,
                                                     IDbTransaction trans = null, int?commandTimeout = null,
                                                     Expression <Func <T, object> > orderbyExpression = null)
            where T : class
        {
            var sqllam     = new SqlExp <T>(db.GetAdapter());
            var countSqlam = new SqlExp <T>(db.GetAdapter());

            if (whereExpression != null)
            {
                sqllam     = sqllam.Where(whereExpression);
                countSqlam = countSqlam.Where(whereExpression);
            }

            if (orderbyExpression != null)
            {
                sqllam = sqllam.OrderBy(orderbyExpression);
            }

            if (groupByexpression != null)
            {
                sqllam = sqllam.GroupBy(groupByexpression);
            }

            countSqlam = countSqlam.Count();

            int countRet;
            var sqlString = countSqlam.SqlString;

            try {
                DebuggingSqlString(sqlString);
                countRet = db.Query <int>(sqlString, countSqlam.Parameters).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = countSqlam.Parameters
                      };
            }
            var sqlstring = sqllam.QueryPage(pageSize, pageNumber);

            try
            {
                DebuggingSqlString(sqlstring);
                var retlist = db.Query <T>(sqlstring, sqllam.Parameters, trans, commandTimeout: commandTimeout);

                return(new PagedResult <T>(retlist, countRet, pageSize, pageNumber));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlstring);
                throw new DapperLamException(ex.Message, ex, sqlstring)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }
コード例 #2
0
        public static PagedResult <TResult> PagedQuery <T, TResult>(this IDbConnection db, int pageSize, int pageNumber, Action <SqlExp <T> > action, IDbTransaction trans = null, int?commandTimeout = null) where T : class  where TResult : class
        {
            var sqllam = new SqlExp <T>(db.GetAdapter());

            var countSqlam = new SqlExp <T>(db.GetAdapter(), true);

            action?.Invoke(sqllam);

            action?.Invoke(countSqlam);

            countSqlam = countSqlam.Count();

            int countRet;

            try
            {
                countRet = db.Query <int>(countSqlam.SqlString, countSqlam.Parameters, trans, commandTimeout: commandTimeout).FirstOrDefault();
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + ex.StackTrace);
                    Debug.WriteLine(countSqlam.SqlString);
                }

                Console.WriteLine(ex.Message + ex.StackTrace);
                Console.WriteLine(countSqlam.SqlString);
                throw new DapperLamException(ex.Message, ex, countSqlam.SqlString)
                      {
                          Parameters = countSqlam.Parameters
                      };
            }

            var sqlstring = sqllam.QueryPage(pageSize, pageNumber);

            try
            {
                var retlist = db.Query <TResult>(sqlstring, sqllam.Parameters, trans, commandTimeout: commandTimeout);
                return(new PagedResult <TResult>(retlist, countRet, pageSize, pageNumber));
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + ex.StackTrace);
                    Debug.WriteLine(sqlstring);
                }

                Console.WriteLine(ex.Message + ex.StackTrace);
                Console.WriteLine(sqlstring);
                throw new DapperLamException(ex.Message, ex, sqlstring)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }
コード例 #3
0
        public static PagedResult <TResult> PagedQuery <T, TResult>(this IDbConnection con, int pageSize, int pageNumber, Action <SqlExp <T> > action, IDbTransaction trans = null, int?commandTimeout = null, string specialSchema = null) where T : class where TResult : class
        {
            var db     = trans == null ? con : trans.Connection;
            var sqllam = new SqlExp <T>(db.GetAdapter(), specialSchema: specialSchema);

            var countSqlam = new SqlExp <T>(db.GetAdapter(), true, specialSchema: specialSchema);

            action?.Invoke(sqllam);

            action?.Invoke(countSqlam);

            countSqlam = countSqlam.Count();

            int countRet;

            var sqlString = countSqlam.SqlString;

            try
            {
                DebuggingSqlString(sqlString);
                countRet = db.Query <int>(countSqlam.SqlString, countSqlam.Parameters, trans, commandTimeout: commandTimeout).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = countSqlam.Parameters
                      };
            }

            var sqlstring = sqllam.QueryPage(pageSize, pageNumber);

            try
            {
                DebuggingSqlString(sqlstring);
                var retlist = db.Query <TResult>(sqlstring, sqllam.Parameters, trans, commandTimeout: commandTimeout);
                return(new PagedResult <TResult>(retlist, countRet, pageSize, pageNumber));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlstring);
                throw new DapperLamException(ex.Message, ex, sqlstring)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }