Asp.net -Repeater用法   发布时间:2017-09-07 14:17:53

Asp.net -Repeater用法 获取分页数据方法(多个排序字段)

/// <summary>

/// 获取分页数据(多个排序字段)

/// </summary>

/// <param name="pageSize">页面大小</param>

/// <param name="pageIndex">当前页索引</param>

/// <param name="recordCount">记录数</param>

/// <param name="where">查询条件</param>

/// <returns></returns>

public DataTable GetListManyOrderFile(int pageIndex, int pageSize, string table, string orderField, int orderType, string where, string key, out int recordCount)

{

if (where == null)

where = "";

OpenConn();

SqlCommand cmd = new SqlCommand("P_Page_ManyOrderFile", conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@tblName", SqlDbType.VarChar, 800);

cmd.Parameters["@tblName"].Value = table;

cmd.Parameters.Add("@fldName", SqlDbType.VarChar, 255);

cmd.Parameters["@fldName"].Value = orderField;

cmd.Parameters.Add("@OrderType", SqlDbType.TinyInt);

cmd.Parameters["@OrderType"].Value = orderType;

cmd.Parameters.Add("pageIndex", SqlDbType.Int);

cmd.Parameters["pageIndex"].Value = pageIndex;

cmd.Parameters.Add("pageSize", SqlDbType.TinyInt);

cmd.Parameters["pageSize"].Value = pageSize;

cmd.Parameters.Add("strWhere", SqlDbType.VarChar, 1500);

cmd.Parameters["strWhere"].Value = where;

cmd.Parameters.Add("@doCount", SqlDbType.TinyInt);

cmd.Parameters["@doCount"].Value = 0;

cmd.Parameters.Add("@key", SqlDbType.VarChar,255);

cmd.Parameters["@key"].Value = key;

SqlDataAdapter DA = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

DA.Fill(ds);

conn.Close();

conn.Dispose();

recordCount = GetRecordCount(table, where);

return ds.Tables[0];

}

返回记录数

/// <summary>

/// 返回记录数

/// </summary>

/// <param name="table">表名</param>

/// <param name="where">查询条件</param>

/// <returns></returns>

public int GetRecordCount(string table, string where)

{

int RecordCount = 0;

OpenConn();

SqlCommand cmd = new SqlCommand("TZFX_ZHDD_P_Page", conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@tblName", SqlDbType.VarChar, 800);

cmd.Parameters["@tblName"].Value = table;

cmd.Parameters.Add("strWhere", SqlDbType.VarChar, 1500);

cmd.Parameters["strWhere"].Value = where;

cmd.Parameters.Add("@doCount", SqlDbType.TinyInt);

cmd.Parameters["@doCount"].Value = 1;

cmd.Prepare();

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

while (dr.Read())

{

RecordCount = int.Parse(dr[0].ToString());

}

//conn.Close();

//conn.Dispose();

dr.Close();

dr.Dispose();

return RecordCount;

}

获取分页数据存储过程(多个排序字段)

USE [表名]

GO

/****** Object: StoredProcedure [dbo].[P_Page_ManyOrderFile] Script Date: 03/06/2013 09:32:23 ******/

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

ALTER PROCEDURE [dbo].[P_Page_ManyOrderFile]

@tblName varchar(800), -- 表名

@strGetFields varchar(1000) = '*', -- 需要返回的列

@fldName varchar(255)='', -- 排序的字段名

@PageSize int = 10, -- 页尺寸

@PageIndex int = 1, -- 页码

@doCount tinyint=0, -- 返回记录总数, 非 0 值则返回

@OrderType tinyint = 0, -- 设置排序类型, 非 0 值则降序

@strWhere varchar(1500) = '',-- 查询条件 (注意: 不要加 where)

@key varchar(255)-- 主键,用于分页

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

--以上代码的意思是如果@doCount传递过来的不是0,,就执行总数统计。以下的所有代码都是@doCount为0的情况:

end

else

begin

if @OrderType != 0

begin

set @strTmp = " not in (select "

set @strOrder = " order by " + @fldName +" desc"

--如果@OrderType不是0,就执行降序,这句很重要!

end

else

begin

set @strTmp = " not in (select "

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 [" + @key + "]" + @strTmp + "["+ @key + "]

from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @key + "]

from " + @tblName + @strOrder + ") as tblTmp)"+ @strOrder

if @strWhere !=''

set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from "

+ @tblName + " where [" + @key + "]" + @strTmp + "["

+ @key + "] from (select top " + str((@PageIndex-1)*@PageSize) + " ["

+ @key + "] from " + @tblName + " where " + @strWhere + " "

+ @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder

end

end

exec (@strSQL)

返回记录数存储过程

USE [表名]

GO

/****** Object: StoredProcedure [dbo].[TZFX_ZHDD_P_Page] Script Date: 03/06/2013 09:55:24 ******/

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

ALTER PROCEDURE [dbo].[TZFX_ZHDD_P_Page]

@tblName varchar(800), -- 表名

@strGetFields varchar(1000) = '*', -- 需要返回的列

@fldName varchar(255)='', -- 排序的字段名

@PageSize int = 10, -- 页尺寸

@PageIndex int = 1, -- 页码

@doCount tinyint=0, -- 返回记录总数, 非 0 值则返回

@OrderType tinyint = 0, -- 设置排序类型, 非 0 值则降序

@strWhere varchar(1500) = ''-- 查询条件 (注意: 不要加 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

--以上代码的意思是如果@doCount传递过来的不是0,,就执行总数统计。以下的所有代码都是@doCount为0的情况:

end

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

beginwww.2cto.com

--以下代码赋予了@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)

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:咸宁网站建设 http://xianning.45qun.com