标签:des style blog io color ar 使用 for sp
USE [OA] GO /****** Object: StoredProcedure [dbo].[spGetByPage] Script Date: 11/05/2014 13:01:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[spGetByPage] ( @tblName varchar(255), -- 表名 @strGetFields varchar(1000) = ‘*‘, -- 需要返回的列 @fldName varchar(255)=‘ID‘, -- 排序的字段名 @PageSize int = 5, -- 页尺寸 @PageIndex int = 1, -- 页码 @doCount bit = 5, -- 返回记录总数, 非 0 值则返回 @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 @strWhere varchar(1500)=‘1=1‘ -- 查询条件 (注意: 不要加 where) ) AS declare @strSQL varchar(5000) -- 主语句 declare @strTmp varchar(110) -- 临时变量 declare @strOrder varchar(400) -- 排序类型 if @doCount != 0 begin if @strWhere !=‘‘ set @strSQL = ‘select count(*) as Total from ‘ + @tblName + ‘ where ‘+@strWhere else set @strSQL = ‘select count(*) as Total from ‘ + @tblName end --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况 else begin if @OrderType != 0 begin set @strTmp = ‘<(select min‘ set @strOrder = ‘ order by ‘ + @fldName +‘ desc‘ --如果@OrderType不是0,就执行降序,这句很重要! end else begin set @strTmp = ‘>(select max‘ set @strOrder = ‘ order by ‘ + @fldName +‘ 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 --如果是第一页就执行以上代码,这样会加快执行速度 end else begin --以下代码赋予了@strSQL以真正执行的SQL代码 set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘ + @tblName + ‘ where ‘ + @fldName + ‘‘ + @strTmp + ‘(‘+ @fldName + ‘) from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ ‘+ @fldName + ‘ from ‘ + @tblName + ‘‘ + @strOrder + ‘) as tblTmp)‘+ @strOrder if @strWhere != ‘‘ set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘ + @tblName + ‘ where ‘ + @fldName + ‘‘ + @strTmp + ‘(‘ + @fldName + ‘) from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ ‘ + @fldName + ‘ from ‘ + @tblName + ‘ where ‘ + @strWhere + ‘ ‘ + @strOrder + ‘) as tblTmp) and ‘ + @strWhere + ‘ ‘ + @strOrder end end exec (@strSQL)
C#调用
/// <summary> /// get balanceServers by page /// </summary> /// <param name="pageSize"></param> /// <param name="pageIndex"></param> /// <param name="totalCount"></param> /// <returns></returns> public List<BalanceServers> GetBalanceServerByPage(int pageSize, int pageIndex, out int totalCount) { List<BalanceServers> balanceList = null; totalCount = 0; string sp = "spGetByPage"; SqlParameter[] spPage ={ new SqlParameter("@tblName","BalanceServers"), new SqlParameter("@strGetFields","*"), new SqlParameter("@fldName","Guid"), new SqlParameter("@PageSize",pageSize), new SqlParameter("@PageIndex",pageIndex), new SqlParameter("@doCount",0), new SqlParameter("@OrderType",0) };try { using (SqlDataReader reader = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, sp, spPage)) { balanceList = CBO.FillCollection<BalanceServers>(reader); } } catch (Exception ex) { log.Info("Error:" + ex); } return balanceList; }
public static List<T> FillCollection<T>(SqlDataReader reader) where T : class, new() { List<T> tlist = new List<T>() ; while (reader.Read()) { if (tlist == null) tlist = new List<T>(); T t = new T(); foreach (PropertyInfo p in t.GetType().GetProperties()) { try { object coloumnValue = reader[p.Name]; Type objDataType = coloumnValue.GetType(); if (coloumnValue == null || coloumnValue == DBNull.Value) { p.SetValue(t, Null.SetNull(p), null); } else { p.SetValue(t, coloumnValue, null); } } catch (Exception) { } } tlist.Add(t); } return tlist; }
标签:des style blog io color ar 使用 for sp
原文地址:http://www.cnblogs.com/kennyliu/p/4076144.html