由于客戶對速度和性能上的要求越來越變態,而數據量一天天的龐大,因此本人產生了數據的查詢和分頁完全由客戶端回調來實現。想法看上去復雜,實現起來也不難。廢話不多說,看程序吧。
一、存儲過程
包頭:
create or replace package h_querypack is
-- author : evorul
-- created : 2007-3-29
-- purpose : 查詢機構表
-- public type declarations
type mycursor is ref cursor;
procedure querylog (ret_cursor out mycursor,errorcode out int,p_logid int,p_starttime date,p_endtime date,p_operator varchar2 ,p_orderfield varchar2 ,
p_desc int,p_pagesize int,p_pageindex int,p_recordcount out int);
end h_querypack;
包體:
create or replace package body h_querypack is
-- author : evorul
-- created : 2007-3-29
-- purpose : 查詢
-- 查詢公司,分頁用
procedure querylog (ret_cursor out mycursor,errorcode out int,p_logid int,p_starttime date,p_endtime date,p_operator varchar2 ,p_orderfield varchar2 ,
p_desc int,p_pagesize int,p_pageindex int,p_recordcount out int)
as
v_sql varchar2(3000);
v_sqlcount varchar2(3000);
v_orderfield varchar2(100);
v_order varchar2(5); --順序
v_count int;
v_heirownum int;
v_lowrownum int;
begin
errorcode:=0;
v_sql:='select * from log where 1=1 ';
if(p_logid <> 0)then
v_sql := v_sql || ' and id = ' || to_char(p_logid);
end if;
if p_operator is not null then then
v_sql := v_sql || 'and operator like ''%' || rtrim(ltrim(p_operator))||'%''';
end if;
v_sql := v_sql ||' and (to_char(time,''yyyymmdd'') between ''' || to_char(p_starttime, 'yyyymmdd') ||''' and ''' || to_char(p_endtime, 'yyyymmdd') ||''')';
----取記錄總數
v_sqlcount := 'select count(*) from (' || v_sql || ')';
execute immediate v_sqlcount into v_count;
p_recordcount := v_count;
--排序字段
if p_orderfield is not null then
v_orderfield:=p_orderfield;
else
v_orderfield:='id';
end if;
--是否降序
if p_desc <>0 then
v_order:=' asc';
else
v_order:=' desc';
end if;
v_sql:=v_sql || 'order by '|| v_orderfield || v_order;
----執行分頁查詢
v_heirownum := p_pageindex * p_pagesize;
v_lowrownum := v_heirownum - p_pagesize + 1;
v_sql := 'select * from (
select a.*, rownum rn from ('|| v_sql ||') a where rownum <= '|| to_char(v_heirownum) || ') b where rn >= ' || to_char(v_lowrownum) ;
open ret_cursor for v_sql;
exception
when no_data_found then
errorcode:=9999;
when others then
errorcode:=9999;
end querylog;
end h_querypack;
二、程序
dataaccess.cs
using system;
using system.data;
using system.data.oracleclient;
using system.collections;
using system.collections.specialized;
/**//// <summary>
///數據層 author: evorul date:2007-03-29
/// </summary>
public class dataaccess
...{
/**//// <summary>
/// 返回數據庫連接字符串
/// </summary>
public static string databaseconnectionstring
...{
get
...{
namevaluecollection configsettings = (namevaluecollection)system.configuration.configurationmanager.getsection("appsettings");
return configsettings["connectionstring"];
}
}
/**//// <summary>
/// 返回每一頁顯示的紀錄數
/// </summary>
public static int rowsperpage
...{
get
...{
namevaluecollection configsettings = (namevaluecollection)system.configuration.configurationmanager.getsection("appsettings");
return convert.toint32(configsettings["rowsperpage"]);
}
}
/**//// <summary>
/// 獲取特定日志集合
/// </summary>
/// <param name="typeid">日志類型</param>
/// <param name="userid">操作人</param>
/// <param name="strorderfield">排序字段</param>
/// <param name="intasc">是否升序 0-降序,1-升</param>
/// <param name="pageindex">頁碼</param>
/// <param name="rowcount">頁行數</param>
/// <param name="recordsum">符合條件的總記錄數</param>
/// <returns></returns>
public static arraylist querylog(string stroperator,datetime dtstarttime,datetime dtendtime, string strorderfield,
int intasc, int pageindex, int rowcount, out int recordsum)
...{
// 返回集合
arraylist myarraylist = new arraylist();
// 創建連接
oracleconnection myconnection = new oracleconnection(databaseconnectionstring);
try
...{
// 打開連接
myconnection.open();
}
catch (exception ex)
...{
throw (ex);
}
try
...{
// 創建存儲過程
oraclecommand mycommand = new oraclecommand("h_querypack.querylog", myconnection);
mycommand.commandtype = commandtype.storedprocedure;
oracledatareader dr;
// ============================== 參數定義 ==============================
// 返回值
mycommand.parameters.add("ret_cursor", oracletype.cursor);
mycommand.parameters["ret_cursor"].direction = parameterdirection.output;
oracleparameter ret = mycommand.parameters.add("errorcode", oracletype.int32);
ret.direction = parameterdirection.output;
oracleparameter retcountsum = mycommand.parameters.addwithvalue("p_recordcount", oracletype.int32);
retcountsum.direction = parameterdirection.output;
// 編號
mycommand.parameters.addwithvalue("p_logid", oracletype.int32).value = 0;
// 用戶編號
mycommand.parameters.addwithvalue("p_operator", oracletype.varchar).value = stroperator;
// 時間下限
mycommand.parameters.addwithvalue("p_starttime", oracletype.datetime).value = dtstarttime;
// 時間上限
mycommand.parameters.addwithvalue("p_endtime", oracletype.datetime).value =dtendtime;
// 排序字段
mycommand.parameters.addwithvalue("p_orderfield", oracletype.varchar).value = strorderfield;
// 怎么排序
mycommand.parameters.addwithvalue("p_desc", oracletype.int32).value = intasc;
// 每頁行數
mycommand.parameters.addwithvalue("p_pagesize", oracletype.int32).value = rowcount;
//頁碼
mycommand.parameters.addwithvalue("p_pageindex", oracletype.int32).value = pageindex;
// ============================ 參數定義完畢 ============================
// 執行存儲過程
dr = mycommand.executereader();
// 執行未成功
if (convert.toint32(ret.value) != 0)
throw new exception("執行存儲過程出錯!");
// 總記錄數
recordsum = convert.toint32(retcountsum.value);
while (dr.read())
...{
// 創建新日志
log log = new log();
//操作業務類型
if (dr["operationtype"] != dbnull.value)
...{
log.operationtype = convert.tostring(dr["operationtype"]);
}
// 時間
if (dr["time"] != dbnull.value)
log.time = convert.todatetime(dr["time"]);
// 用戶
if (dr["operator"] != dbnull.value)
...{
log.operator = convert.tostring(dr["operator"]);
}
// 信息
if (dr["info"] != dbnull.value)
log.info = convert.tostring(dr["info"]);
// 加入返回集合
myarraylist.add(log);
}
dr.close();
return myarraylist;
}
catch (exception ex)
...{
throw (ex);
}
finally
...{
myconnection.close();
}
}
}
datalogic.cs
using system;
using system.data;
using system.configuration;
using system.collections;
/**//// <summary>
/// 業務邏輯層 author: evorul date:2007-03-29
/// </summary>
public class datalogic
...{
public datalogic()
...{
}
public static int recordsum = 0;
/**//// <summary>
/// 查詢日志
/// </summary>
/// <param name="stroperator">操作人</param>
/// <param name="dtstartime">時間范圍下限</param>
/// <param name="dtendtime">時間上限</param>
/// <param name="pageid">頁碼</param>
/// <returns></returns>
public static ienumerable getlogdata(string stroperator,datetime dtstartime,datetime dtendtime, string pageid)
...{
return log.getlist(stroperator,dtstartime,dtendtime,"time",1, convert.toint32(pageid),dataaccess.rowsperpage,out recordsum);
}
}
log.cs
using system;
using system.data;
using system.collections;
/**//// <summary>
/// 日志類
/// </summary>
public class log
...{
// ============================== 成員 ==============================
protected string operationtype;
/**//// <summary>
/// 時間
/// </summary>
protected datetime time = new datetime();
/**//// <summary>
/// 用戶
/// </summary>
protected string m_operator;
/**//// <summary>
/// 信息
/// </summary>
protected string info = "";
// ============================== 屬性 ==============================
public string operationtype
...{
get ...{ return operationtype; }
set ...{ operationtype = value; }
}
/**//// <summary>
/// 時間
/// </summary>
public datetime time
...{
get ...{ return time; }
set ...{ time = value; }
}
/**//// <summary>
/// 用戶
/// </summary>
public string operator
...{
get ...{ return m_operator; }
set ...{ m_operator = value; }
}
/**//// <summary>
/// 信息
/// </summary>
public string info
...{
get ...{ return info; }
set ...{ info = value; }
}
// ============================== 方法 ==============================
/**//// <summary>
/// 創建空日志實例
/// </summary>
public log()
...{
}
/**//// <summary>
/// 新增日志
/// </summary>
public void add()
...{
try
...{
// 暫不支持該方法
throw new exception("新增日志");
}
catch (exception ex)
...{
throw (ex);
}
}
/**//// <summary>
/// 修改日志(不支持)
/// </summary>
public void modify()
...{
// 暫不支持該方法
throw new exception("修改日志");
}
/**//// <summary>
/// 刪除日志
/// </summary>
public void del()
...{
// 暫不支持該方法
throw new exception("修改日志");
}
/**//// <summary>
/// 獲取特定的日志集
/// </summary>
/// <param name="stroperator">操作人</param>
/// <param name="dtstarttime">開始時間</param>
/// <param name="dtendtime">結束時間</param>
/// <param name="strorderfield">排序字段</param>
/// <param name="intasc">0-降序,1-升序</param>
/// <param name="pageindex">頁碼</param>
/// <param name="rowcount">頁行數</param>
/// <param name="recordsum">總記錄數</param>
/// <returns></returns>
public static arraylist getlist(string stroperator,datetime dtstarttime,datetime dtendtime,string strorderfield,int intasc, int pageindex, int rowcount, out int recordsum)
...{
return dataaccess.querylog(stroperator,dtstarttime,dtendtime,strorderfield, intasc, pageindex, rowcount, out recordsum);
}
}
前臺頁 default.aspx
<%[email protected] page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>客戶端回調</title>
<style type="text/css">...
body {...}{
font-size: 12px;
color: #525252;
}
td {...}{
font-size: 12px;
color: #525252;
}
th {...}{
font-size: 12px
}
a:link {...}{
color: #000000; text-decoration: none
}
a:visited {...}{
color: #525252; text-decoration: none
}
a:hover {...}{
color: #0095a7; text-decoration: underline
}
td.alt_1 {...}{
border-top: 1px solid #d6d6d6;
border-right: 1px solid #d6d6d6;
font-size:12px;
color: #4f6b72;
}
td.alt_2 {...}{
border-top: 1px solid #d6d6d6;
border-right: 1px solid #d6d6d6;
}
td.alt_3 {...}{
border-left: 1px solid #d6d6d6;
border-bottom: 1px solid #d6d6d6;
}
td.alt_4 {...}{
border-left: 1px solid #d6d6d6;
border-right: 1px solid #d6d6d6;
}
</style>
<script type="text/javascript">...
//author: evorul date:2007-03-25
var pageindex=1;
function queryserver(objoperator,objstarttime,objendtime,intindex,boolreset)
...{
context = gridspan;
context.innerhtml = "<img src='http://www.survivalescaperooms.com/htmldata/images/pie.gif' />數據加載中...";
arg = "servermethodquery|" + objoperator.value.replace(/$/g,"")+"$"+ objstarttime.value.replace(/$/g,"")+"$"+ objendtime.value.replace(/$/g,"") +"$"+ intindex.tostring().replace(/$/g,"")+"$"+ boolreset.tostring().replace(/$/g,"");
<%= clientscript.getcallbackeventreference(this, "arg", "receiveserverdata", "context")%>;
}
function receiveserverdata(result, context)
...{
context.innerhtml = (result.split('$'))[0];
var t1=document.getelementbyid("recordsum");
var t2=document.getelementbyid("pagesum");
var t3=document.getelementbyid("currentpage");
var t5=document.getelementbyid("linkup");
var t6=document.getelementbyid("linkdown");
var t7=document.getelementbyid("downlistindex");
t1.innerhtml = (result.split('$'))[1];
t2.innerhtml = (result.split('$'))[2];
t3.innerhtml = (result.split('$'))[3];
pageindex=eval((result.split('$'))[3]);
if(pageindex>1)
...{
t5.innerhtml="<a href="#" +(pageindex-1).tostring()+",'false')">上一頁</a>";
}
else
t5.innerhtml = "上一頁";
if(pageindex< eval((result.split('$'))[2]))
...{
t6.innerhtml="<a href="#" +(pageindex+1).tostring()+",'false')">下一頁</a>";
}
else
t6.innerhtml="下一頁";
if(result.split('$').length>4)
...{
var t4=document.getelementbyid("spanindex");
t4.innerhtml = (result.split('$'))[4];
}
t7.value=pageindex;
}
function functionpageload()
...{
if(document.readystate!="complete") return;
context = gridspan;
arg = "servermethodquery|" +"$"+"1753-1-1"+ "$"+"9999-12-31"+ "$"+ "1"+"$"+ "true";
<%= clientscript.getcallbackeventreference(this, "arg", "receiveserverdata", "context")%>;
//頁面加載完后執行的代碼
}
//頁面加載狀態改變時執行的方法
document.onreadystatechange=functionpageload;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" border="0" id="table1">
<tr>
<td align="center" valign="top">
<br />
<table cellpadding="0" cellspacing="0" border="0" width="95%">
<tr>
<td><img src="http://www.survivalescaperooms.com/htmldata/images/round-1.gif" width="13" height="30" alt="" /></td>
<td class="alt_1">
日志查詢</td>
</tr>
<tr>
<td class="alt_4" valign="top" colspan="2" align="center">
<table cellpadding="0" cellspacing="0" border="0" width="95%">
<tr>
<td align="right">
操作人員:</td>
<td align="left" >
<asp:textbox id="txtoperator" runat="server" cssclass="border" readonly="false"
width="90px"></asp:textbox></td>
<td align="right">
操作時間:</td>
<td align="left">
<asp:textbox id="txtstarttime" runat="server" cssclass="border"
readonly="false" width="90px"></asp:textbox>-<asp:textbox
id="txtendtime" runat="server" cssclass="border"
readonly="false" width="90px"></asp:textbox></td>
</tr>
<tr>
<td colspan="4" ></td>
</tr>
<tr>
<td colspan="4" >
<asp:button id="submit" runat="server" cssclass="btn2" text=" 確定 " height="20px" width="50px" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td class="alt_3"> </td>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
<span id="gridspan">
<asp:gridview id="logs" runat="server" autogeneratecolumns="false"
gridlines="horizontal" pagesize="15"
width="95%" backcolor="white" bordercolor="#336666" borderstyle="double" borderwidth="3px" cellpadding="4">
<rowstyle forecolor="#333333" height="24px" backcolor="white" />
<selectedrowstyle bordercolor="red" backcolor="#339966" font-bold="true" forecolor="white" />
<headerstyle backcolor="#336666" forecolor="white" height="30px" font-bold="true" />
<alternatingrowstyle borderwidth="1px" />
<columns>
<asp:templatefield headertext="用戶">
<itemtemplate>
<asp:hyperlink id="hyperlink1" runat="server" navigateurl="" text='<%# eval("userinfo.name") %>'></asp:hyperlink>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="類型">
<itemtemplate>
<asp:hyperlink id="hyperlink2" runat="server" navigateurl="" text='<%# eval("type.name") %>'></asp:hyperlink>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="時間">
<itemtemplate>
<asp:hyperlink id="hyperlink4" runat="server" navigateurl="" text='<%# eval("time") %>'></asp:hyperlink>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="備注">
<itemtemplate>
<asp:hyperlink id="hyperlink5" runat="server" navigateurl="" text='<%# eval("info") %>'></asp:hyperlink>
</itemtemplate>
</asp:templatefield>
</columns>
<footerstyle backcolor="white" forecolor="#333333" />
<pagerstyle backcolor="#336666" forecolor="white" horizontalalign="center" />
</asp:gridview>
</span>
<table border="0" cellpadding="0" cellspacing="0" bordercolorlight="#000000" bordercolordark="#ffffff">
<tr>
<td align="center" nowrap >
共有<asp:label id="recordsum" runat="server" />條<asp:label id="pagesum" runat="server" />頁結果
當前顯示為第<asp:label id="currentpage" runat="server" />頁 15條/頁
<asp:label id="linkup" runat="server" >上一頁</asp:label>
<asp:label id="linkdown" runat="server" >下一頁</asp:label>
跳轉至第<span id="spanindex"><asp:dropdownlist id="downlistindex" runat="server" width="50px">
</asp:dropdownlist></span>頁
</td>
</tr>
</table>
<asp:objectdatasource id="datasourcelog" runat="server" typename="datalogic" selectmethod="getlogdata">
<selectparameters>
<asp:controlparameter controlid="txtoperator" defaultvalue=" " name="stroperator" propertyname="text"
type="string" />
<asp:controlparameter controlid="txtstarttime" defaultvalue="1753-1-1" name="dtstartime"
propertyname="text" type="datetime" />
<asp:controlparameter controlid="txtendtime" defaultvalue="9999-12-31" name="dtendtime"
propertyname="text" type="datetime" />
<asp:controlparameter name="pageid" controlid="downlistindex" defaultvalue="1" propertyname="selectedvalue" type="string" />
</selectparameters>
</asp:objectdatasource>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
default.aspx.cs
using system;
using system.data;
using system.configuration;
using system.web;
using system.io;
using system.text;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.globalization;
public partial class _default : system.web.ui.page,icallbackeventhandler
...{
protected void page_load(object sender, eventargs e)
...{
this.submit.attributes.add("onclick", "queryserver(txtoperator,txtstarttime,txtendtime,1,"true");return false;");
this.downlistindex.attributes.add("onchange", "queryserver(txtoperator,txtstarttime,txtendtime,this.value,"false");return false;");
}
回調分頁#region 回調分頁
private string serverreturn;
public string getcallbackresult()
...{
string[] parts = serverreturn.split('|');
//根據傳遞的方法名進行調用,并傳遞相應的參數,目前只支持一個參數
return (string)gettype().getmethod(parts[0]).invoke(this, new object[] ...{ parts[1] });
}
public void raisecallbackevent(string eventargument)
...{
serverreturn = eventargument;
}
/**//// <summary>
/// 根據從客戶端傳來的值,對gridview的內容進行更新,并將更新后的gridview的html返回
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
public string servermethodquery(string arg)
...{
logs.datasourceid = "datasourcelog";
string[] arrayarg = arg.split('$');
this.txtoperator.text = arrayarg[0];
this.txtstarttime.text= arrayarg[1];
this.txtendtime.text = arrayarg[2];
intialpageselect();
this.downlistindex.selectedvalue = arrayarg[3];
logs.databind();
//傳入客戶端字符串,并用"$"分割
stringbuilder strhtml = new stringbuilder();
strhtml.append(rendercontrol(logs));
strhtml.append("$");
strhtml.append(datalogic.recordsum.tostring());
strhtml.append("$");
strhtml.append(convert.tostring(datalogic.recordsum / dataaccess.rowsperpage + 1));
strhtml.append("$");
strhtml.append(arrayarg[3]);
if (arrayarg[4] == "true")
...{
strhtml.append("$");
intialpageselect();
strhtml.append(rendercontrol(downlistindex));
}
return strhtml.tostring();
}
private string rendercontrol(control control)
...{
stringwriter writer1 = new stringwriter(cultureinfo.invariantculture);
htmltextwriter writer2 = new htmltextwriter(writer1);
control.rendercontrol(writer2);
writer2.flush();
writer2.close();
return writer1.tostring();
}
/**//// <summary>
/// 初始化頁下拉單
/// </summary>
private void intialpageselect()
...{
downlistindex.items.clear();
for (int i = 0; i < (datalogic.recordsum / dataaccess.rowsperpage + 1); i++)
...{
this.downlistindex.items.add(convert.tostring(i + 1));
}
}
#endregion
}
如有錯誤,歡迎指正!
新聞熱點
疑難解答