if @strWhere != '' if @OrderType != 0
begin set @strTmp = '<(select min' set @strOrder = ' order by [' + @OrderfldName +'] desc'--如果@OrderType不是0,,就執(zhí)行降序,這句很重要,! end else begin set @strTmp = '>(select max' set @strOrder = ' order by [' + @OrderfldName +'] asc' end if @PageIndex = 1 begin if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder else set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @tblName + ' '+ @strOrder--如果是第一頁就執(zhí)行以上代碼,,這樣會加快執(zhí)行速度 end else begin--以下代碼賦予了@strSQL以真正執(zhí)行的SQL代碼 set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where [' + @OrderfldName + ']' + @strTmp + '(['+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @OrderfldName + '] from ' + @tblName + ' ' + @strOrder + ') as tblTmp)'+ @strOrder if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where [' + @OrderfldName + ']' + @strTmp + '([' + @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @OrderfldName + '] from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder end exec (@strSQL) ----------------------------------------------------------------------
SQLHelper類中寫執(zhí)行存儲過程 這是我的數(shù)據(jù)庫操作類.方法有點老,還是三層架構(gòu).(mvc也應(yīng)該差不多.我還木有試試.)
復(fù)制代碼 代碼如下: /// <summary> /// 分頁(存儲過程) 用法例如下: /// </summary> /// <param>表名(注意:可以多表鏈接)</param> /// <param>需要返回的列</param> /// <param>主鍵</param> /// <param>排序的字段名</param> /// <param>頁尺寸</param> /// <param>頁碼</param> /// <param>查詢到的記錄數(shù)</param> /// <param>設(shè)置排序類型, desc、asc</param> /// <param>查詢條件 (注意: 不要加 where)</param> /// <returns></returns> public static DataTable DbPager( string tblName, string strGetFields, string PkeyfldName, string OrderfldName, int PageSize, int PageIndex, out int doCount, string OrderType, string strWhere) { SqlConnection con = new SqlConnection(ConnString.Value); SqlDataAdapter da = new SqlDataAdapter("DbPager", con); da.SelectCommand.CommandType = CommandType.StoredProcedure; da.SelectCommand.Parameters.Add(new SqlParameter("@tblName", tblName)); da.SelectCommand.Parameters.Add(new SqlParameter("@strGetFields", strGetFields)); da.SelectCommand.Parameters.Add(new SqlParameter("@PkeyfldName", PkeyfldName)); da.SelectCommand.Parameters.Add(new SqlParameter("@OrderfldName", OrderfldName)); da.SelectCommand.Parameters.Add(new SqlParameter("@PageSize", PageSize)); da.SelectCommand.Parameters.Add(new SqlParameter("@PageIndex", PageIndex)); da.SelectCommand.Parameters.Add("@doCount", SqlDbType.Int); da.SelectCommand.Parameters["@doCount"].Direction = ParameterDirection.Output; da.SelectCommand.Parameters.Add(new SqlParameter("@OrderType", OrderType)); da.SelectCommand.Parameters.Add(new SqlParameter("@strWhere", strWhere)); DataSet ds = new DataSet(); da.Fill(ds); doCount = Convert.ToInt32(da.SelectCommand.Parameters["@doCount"].Value); return ds.Tables[0]; } |
|