コード例 #1
0
 public DataTable FindTable(string strSql)
 {
     using (DbConnection conn = new MySqlConnection(DbHelper.connstring))
     {
         var IDataReader = new DbHelper(conn).ExecuteReader(strSql);
         return(ConvertExtension.IDataReaderToDataTable(IDataReader));
     }
 }
コード例 #2
0
        public IEnumerable <T> FindListBySql <T>(string strSql, DbParameter[] dbParameter, string orderField, bool isAsc, int pageSize, int pageIndex, out int total)
        {
            using (DbConnection conn = new MySqlConnection(DbHelper.connstring))
            {
                StringBuilder sb = new StringBuilder();
                if (pageIndex == 0)
                {
                    pageIndex = 1;
                }
                int    num     = (pageIndex - 1) * pageSize;
                int    num1    = (pageIndex) * pageSize;
                string OrderBy = "";

                if (!string.IsNullOrEmpty(orderField))
                {
                    if (orderField.ToUpper().IndexOf("ASC") + orderField.ToUpper().IndexOf("DESC") > 0)
                    {
                        OrderBy = " Order By " + orderField;
                    }
                    else
                    {
                        OrderBy = " Order By " + orderField + " " + (isAsc ? "ASC" : "DESC");
                    }
                }
                else
                {
                    OrderBy = " order by (select 0)";
                }
#if SQLSERVER
                sb.Append("Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
                sb.Append(" As rowNum, * From (" + strSql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
#endif
                sb.Append(strSql);
                sb.Append(OrderBy);
                sb.AppendFormat(" limit {0},{1}", num, pageSize);
                total = Convert.ToInt32(new DbHelper(conn).ExecuteScalar("Select Count(1) From (" + strSql + ") As t"));
                IDataReader rdr = new DbHelper(conn).ExecuteReader(sb.ToString());
                return(ConvertExtension.IDataReaderToList <T>(rdr));
            }
        }