通用分页显示查询存储过程

通用分页显示查询存储过程

/*
功能描述: 通用分页显示查询
条件:表中不存在标示递增字段
输入参数:
@tblName: 表名
@strGetFields: 需要返回的列 '*':返回所以列信息
@fldName: 排序的字段名
@OrderType: 设置排序类型, 非 0 值则降序
@PageSize: 页尺寸
@PageIndex: 页码
@doCount: 返回记录总数, 非 0 值则返回
@strOrderBy: 缺省排序字段信息 (注意: 不要加 ORDER BY)
格式: Field1 DESC,Field2 ASC,
@strWhere: 查询条件 (注意: 不要加 WHERE)
输出参数: @RecordCount: 记录总数
更改纪录:
*/
ALTER PROCEDURE Pagination2
(
@tblName varchar(255),
@strGetFields varchar(1000) = '*',
@fldName varchar(255) = '',
@PageSize int = 10,
@PageIndex int = 1,
@doCount bit = 0,
@OrderType bit = 0,
@strOrderBy varchar(500) = '',
@strWhere varchar(1500) = '',
@RecordCount int output
)
AS
DECLARE @strSQL varchar(5000) — 主语句
DECLARE @strTmp varchar(200) — 临时变量
DECLARE @strOrder varchar(400) — 排序变量

–如果@doCount传递过来的不是0,就执行总数统计
IF (@doCount != 0)
BEGIN
DECLARE @sWhere varchar(2000)

SET @sWhere = ''
IF (@strWhere != '')
SET @sWhere = ' WHERE ' + @strWhere

SET @strSQL = 'if exists (select * from dbo.sysobjects where id = object_id(''[dbo].[tmpTable]'') and OBJECTPROPERTY(id, ''IsUserTable'') = 1) '
SET @strSQL = @strSQL + ' UPDATE tmpTable SET Total = (SELECT COUNT(*) FROM [' + @tblName + '] ' + @sWhere + ') '
SET @strSQL = @strSQL + ' ELSE SELECT COUNT(*) AS Total INTO tmpTable FROM [' + @tblName + '] ' + @sWhere

EXEC (@strSQL)

SELECT @RecordCount=Total FROM tmpTable

–删除总数统计临时表
EXEC ('DROP TABLE tmpTable')
END

–PRINT @RecordCount

–如果@OrderType不是0,就执行降序
IF (@OrderType != 0)
BEGIN
SET @strTmp = '  (SELECT MAX'
SET @strOrder = ' ORDER BY ' + @strOrderBy + '[' + @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
–为搜索表建立自动编号 保存到临时表中
SET @strSQL = 'SELECT TOP ' + str(@PageIndex*@PageSize) + ' IDENTITY(int,1,1) AS IID, ' + @strGetFields + ' INTO #tmpTable FROM [' + @tblName + ']'
IF @strWhere != ''
SET @strSQL = @strSQL + ' WHERE ' + @strWhere + ' ' + @strOrder
ELSE
SET @strSQL = @strSQL + @strOrder

–以下代码赋予了@strSQL以真正执行的SQL代码
SET @strSQL = @strSQL + ' SELECT TOP ' + str(@PageSize) + ' ' + @strGetFields + ' FROM #tmpTable'
+ ' WHERE IID ' + @strTmp + '(IID) FROM (SELECT TOP ' + str((@PageIndex-1)*@PageSize) + ' IID FROM #tmpTable) AS tblTmp) DROP TABLE #tmpTable'
END

–PRINT @strSQL

–执行分页查询
EXEC (@strSQL)

页面无刷新的PostBack事件

页面无刷新的PostBack事件

TextBox的onchange事件会引起页面重新刷新,这使得输入很不方便,于是我想了以下的解决办法。
思路:1、调用简单的前台脚本打开一个新页, 2、把相关参数传递给该新页处理(该新页的前后台均可处理), 3、处理后所得数据再用前台脚本返回opener, 4、数据处理完毕后被打开的页面自动关闭。代码例:要处理的页面send.aspx:
string windowAttribs = "width=10px," + "height=10px," + "left='+((screen.width +" + "10" + ") * 2)+'," + "top='+ (screen.height + " + "10" + ") * 2+'";

TextBox2.Attributes["onchange"] = "var aaa = this.value;window.open('open_XML.aspx?get_id=TextBox2&get_value='+aaa,'open_test','"+windowAttribs+"')";

TextBox3.Attributes["onchange"] = "var aaa = this.value;window.open('open_page.aspx?get_id=TextBox3&get_value='+aaa,'open_test','"+windowAttribs+"')";

打开的页面open_page.aspx:
private void Page_Load(object sender, System.EventArgs e)

{

object ls_o1 = Request.QueryString["get_id"].ToString();

object ls_o2 = Request.QueryString["get_value"].ToString();

if(ls_o1 != null && ls_o2 != null)

{

get_id.Value = (string)ls_o1;

get_value.Value = (string)ls_o2;

}

send_value_back();

}

private void send_value_back()

{

if(get_id.Value != "" && get_value.Value != null)

{

this.Response.Write("window.opener.Form1."+get_id.Value+".value='aaaaaaaaaaaa'"+"");

this.Response.Write("window.opener=null;window.close();");

}

}