|
由于客户对速度和性能上的要求越来越变态,而数据量一天天的庞大,因此本人产生了数据的查询和分页完全由客户端回调来实现。想法看上去复杂,实现起来也不难。废话不多说,看程序吧。 hHKLinux联盟 hHKLinux联盟 一、存储过程 hHKLinux联盟 hHKLinux联盟 包头: hHKLinux联盟 hHKLinux联盟 create or replace package H_QUERYPACK is hHKLinux联盟 hHKLinux联盟 -- Author : Evorul hHKLinux联盟 -- Created : 2007-3-29 hHKLinux联盟 -- Purpose : 查询机构表 hHKLinux联盟 hHKLinux联盟 -- Public type declarations hHKLinux联盟 type MYCURSOR is REF CURSOR; hHKLinux联盟 PROCEDURE QUERYLOG (RET_CURSOR OUT MYCURSOR,ERRORCODE OUT INT,p_logID int,p_StartTime Date,p_EndTime Date,p_Operator varchar2 ,p_OrderField varchar2 , hHKLinux联盟 p_Desc int,p_PageSize Int,p_PageIndex Int,p_RecordCount Out Int); hHKLinux联盟 hHKLinux联盟 end H_QUERYPACK; hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 包体: hHKLinux联盟 hHKLinux联盟 create or replace package body H_QUERYPACK Is hHKLinux联盟 -- Author : Evorul hHKLinux联盟 -- Created : 2007-3-29 hHKLinux联盟 -- Purpose : 查询 hHKLinux联盟 hHKLinux联盟 -- 查询公司,分页用 hHKLinux联盟 PROCEDURE QUERYLOG (RET_CURSOR OUT MYCURSOR,ERRORCODE OUT INT,p_logID int,p_StartTime Date,p_EndTime Date,p_Operator varchar2 ,p_OrderField varchar2 , hHKLinux联盟 p_Desc int,p_PageSize Int,p_PageIndex Int,p_RecordCount Out Int) hHKLinux联盟 AS hHKLinux联盟 v_sql varchar2(3000); hHKLinux联盟 v_sqlcount varchar2(3000); hHKLinux联盟 v_orderfield varchar2(100); hHKLinux联盟 v_order VARCHAR2(5); --顺序 hHKLinux联盟 v_count int; hHKLinux联盟 v_heiRownum int; hHKLinux联盟 v_lowRownum int; hHKLinux联盟 BEGIN hHKLinux联盟 ERRORCODE:=0; hHKLinux联盟 hHKLinux联盟 v_sql:='select * from LOG Where 1=1 '; hHKLinux联盟 hHKLinux联盟 if(p_logID <> 0)then hHKLinux联盟 v_sql := v_sql || ' and id = ' || TO_CHAR(p_logID); hHKLinux联盟 end if; hHKLinux联盟 hHKLinux联盟 IF p_Operator Is Not Null Then then hHKLinux联盟 v_sql := v_sql || 'And operator LIKE ''%' || RTRIM(LTRIM(p_Operator))||'%'''; hHKLinux联盟 end if; hHKLinux联盟 hHKLinux联盟 v_sql := v_sql ||' and (TO_CHAR(time,''YYYYMMDD'') between ''' || to_char(p_StartTime, 'YYYYMMDD') ||''' and ''' || to_char(p_EndTime, 'YYYYMMDD') ||''')'; hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 ----取记录总数 hHKLinux联盟 v_sqlcount := 'select count(*) from (' || v_sql || ')'; hHKLinux联盟 execute immediate v_sqlcount into v_count; hHKLinux联盟 p_RecordCount := v_count; hHKLinux联盟 hHKLinux联盟 --排序字段 hHKLinux联盟 IF p_OrderField IS NOT NULL THEN hHKLinux联盟 v_orderfield:=p_OrderField; hHKLinux联盟 Else hHKLinux联盟 v_orderfield:='ID'; hHKLinux联盟 END IF; hHKLinux联盟 --是否降序 hHKLinux联盟 IF p_Desc <>0 THEN hHKLinux联盟 v_order:=' ASC'; hHKLinux联盟 Else hHKLinux联盟 v_order:=' DESC'; hHKLinux联盟 END IF; hHKLinux联盟 hHKLinux联盟 v_sql:=v_sql || 'ORDER BY '|| v_orderfield || v_order; hHKLinux联盟 ----执行分页查询 hHKLinux联盟 v_heiRownum := p_PageIndex * p_PageSize; hHKLinux联盟 v_lowRownum := v_heiRownum - p_PageSize + 1; hHKLinux联盟 hHKLinux联盟 v_sql := 'SELECT * FROM ( hHKLinux联盟 SELECT A.*, rownum rn FROM ('|| v_sql ||') A WHERE rownum <= '|| to_char(v_heiRownum) || ') B WHERE rn >= ' || to_char(v_lowRownum) ; hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 OPEN RET_CURSOR FOR v_sql; hHKLinux联盟 hHKLinux联盟 EXCEPTION hHKLinux联盟 WHEN NO_DATA_FOUND THEN hHKLinux联盟 ERRORCODE:=9999; hHKLinux联盟 WHEN OTHERS THEN hHKLinux联盟 ERRORCODE:=9999; hHKLinux联盟 END QUERYLOG; hHKLinux联盟 hHKLinux联盟 END H_QUERYPACK; hHKLinux联盟 二、程序 hHKLinux联盟 hHKLinux联盟 DataAccess.cs hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 using System; hHKLinux联盟 using System.Data; hHKLinux联盟 using System.Data.OracleClient; hHKLinux联盟 using System.Collections; hHKLinux联盟 using System.Collections.Specialized; hHKLinux联盟 hHKLinux联盟 /**//// <summary> hHKLinux联盟 ///数据层 author: EvoRul date:2007-03-29 hHKLinux联盟 /// </summary> hHKLinux联盟 public class DataAccess hHKLinux联盟 ...{ hHKLinux联盟 hHKLinux联盟 /**//// <summary> hHKLinux联盟 /// 返回数据库连接字符串 hHKLinux联盟 /// </summary> hHKLinux联盟 public static String DatabaseConnectionString hHKLinux联盟 ...{ hHKLinux联盟 get hHKLinux联盟 ...{ hHKLinux联盟 NameValueCollection configSettings = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings"); hHKLinux联盟 return configSettings["connectionString"]; hHKLinux联盟 } hHKLinux联盟 } hHKLinux联盟 hHKLinux联盟 /**//// <summary> hHKLinux联盟 /// 返回每一页显示的纪录数 hHKLinux联盟 /// </summary> hHKLinux联盟 public static int RowsPerPage hHKLinux联盟 ...{ hHKLinux联盟 get hHKLinux联盟 ...{ hHKLinux联盟 NameValueCollection configSettings = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings"); hHKLinux联盟 return Convert.ToInt32(configSettings["rowsPerPage"]); hHKLinux联盟 } hHKLinux联盟 } hHKLinux联盟 hHKLinux联盟 /**//// <summary> hHKLinux联盟 /// 获取特定日志集合 hHKLinux联盟 /// </summary> hHKLinux联盟 /// <param name="typeID">日志类型</param> hHKLinux联盟 /// <param name="userID">操作人</param> hHKLinux联盟 /// <param name="strOrderField">排序字段</param> hHKLinux联盟 /// <param name="intASC">是否升序 0-降序,1-升</param> hHKLinux联盟 /// <param name="PageIndex">页码</param> hHKLinux联盟 /// <param name="rowCount">页行数</param> hHKLinux联盟 /// <param name="recordSum">符合条件的总记录数</param> hHKLinux联盟 /// <returns></returns> hHKLinux联盟 public static ArrayList QueryLog(string strOperator,DateTime dtStartTime,DateTime dtEndTime, string strOrderField, hHKLinux联盟 int intASC, int PageIndex, int rowCount, out int recordSum) hHKLinux联盟 ...{ hHKLinux联盟 // 返回集合 hHKLinux联盟 ArrayList myArrayList = new ArrayList(); hHKLinux联盟 hHKLinux联盟 // 创建连接 hHKLinux联盟 OracleConnection myConnection = new OracleConnection(DatabaseConnectionString); hHKLinux联盟 hHKLinux联盟 try hHKLinux联盟 ...{ hHKLinux联盟 // 打开连接 hHKLinux联盟 myConnection.Open(); hHKLinux联盟 } hHKLinux联盟 catch (Exception ex) hHKLinux联盟 ...{ hHKLinux联盟 throw (ex); hHKLinux联盟 } hHKLinux联盟 hHKLinux联盟 try hHKLinux联盟 ...{ hHKLinux联盟 // 创建存储过程 hHKLinux联盟 OracleCommand myCommand = new OracleCommand("H_QUERYPACK.QUERYLOG", myConnection); hHKLinux联盟 myCommand.CommandType = CommandType.StoredProcedure; hHKLinux联盟 OracleDataReader dr; hHKLinux联盟 hHKLinux联盟 // ============================== 参数定义 ============================== hHKLinux联盟 hHKLinux联盟 // 返回值 hHKLinux联盟 myCommand.Parameters.Add("RET_CURSOR", OracleType.Cursor); hHKLinux联盟 myCommand.Parameters["RET_CURSOR"].Direction = ParameterDirection.Output; hHKLinux联盟 OracleParameter ret = myCommand.Parameters.Add("ERRORCODE", OracleType.Int32); hHKLinux联盟 ret.Direction = ParameterDirection.Output; hHKLinux联盟 hHKLinux联盟 OracleParameter retCountSum = myCommand.Parameters.AddWithValue("p_RecordCount", OracleType.Int32); hHKLinux联盟 retCountSum.Direction = ParameterDirection.Output; hHKLinux联盟 hHKLinux联盟 // 编号 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_logID", OracleType.Int32).Value = 0; hHKLinux联盟 hHKLinux联盟 // 用户编号 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_Operator", OracleType.VarChar).Value = strOperator; hHKLinux联盟 hHKLinux联盟 // 时间下限 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_StartTime", OracleType.DateTime).Value = dtStartTime; hHKLinux联盟 hHKLinux联盟 // 时间上限 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_EndTime", OracleType.DateTime).Value =dtEndTime; hHKLinux联盟 hHKLinux联盟 // 排序字段 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_OrderField", OracleType.VarChar).Value = strOrderField; hHKLinux联盟 hHKLinux联盟 // 怎么排序 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_Desc", OracleType.Int32).Value = intASC; hHKLinux联盟 hHKLinux联盟 // 每页行数 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_PageSize", OracleType.Int32).Value = rowCount; hHKLinux联盟 hHKLinux联盟 //页码 hHKLinux联盟 myCommand.Parameters.AddWithValue("p_PageIndex", OracleType.Int32).Value = PageIndex; hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 // ============================ 参数定义完毕 ============================ hHKLinux联盟 hHKLinux联盟 // 执行存储过程 hHKLinux联盟 dr = myCommand.ExecuteReader(); hHKLinux联盟 hHKLinux联盟 // 执行未成功 hHKLinux联盟 if (Convert.ToInt32(ret.Value) != 0) hHKLinux联盟 throw new Exception("执行存储过程出错!"); hHKLinux联盟 hHKLinux联盟 // 总记录数 hHKLinux联盟 recordSum = Convert.ToInt32(retCountSum.Value); hHKLinux联盟 hHKLinux联盟 hHKLinux联盟 while (dr.Read()) hHKLinux联盟 ...{ hHKLinux联盟 // 创建新日志 hHKLinux联盟 Log log = new Log(); hHKLinux联盟 hHKLinux联盟 //操作业务类型 hHKLinux联盟 if (dr["operationtype"] != DBNull.Value) hHKLinux联盟 ...{ hHKLinux联盟 log.OperationType = Convert.ToString(dr["operationtype"]); hHKLinux联盟 } hHKLinux联盟 // 时间 hHKLinux联盟 if (dr["time"] != DBNull.Value) hHKLinux联盟 log.Time = Convert.ToDateTime(dr["time"]); hHKLinux联盟 hHKLinux联盟 // 用户 hHKLinux联盟 if (dr["operator"] != DBNull.Value) hHKLinux联盟 ...{ hHKLinux联盟 log.Operator = Convert.ToString(dr["operator"]); hHKLinux联盟 } hHKLinux联盟 hHKLinux联盟 // 信息 hHKLinux联盟 if (dr["info"] != DBNull.Value) hHKLinux联盟 log.Info = Convert.ToString(dr["info"]); hHKLinux联盟 hHKLinux联盟 // 加入返回集合 hHKLinux联盟 myArrayList.Add(log); hHKLinux联盟 } hHKLinux联盟 hHKLinux联盟 dr.Close(); hHKLinux联盟 return myArrayList; hHKLinux联盟 } hHKLinux联盟 catch (Exception ex) hHKLinux联盟 ...{ hHKLinux联盟 throw (ex); hHKLinux联盟 } hHKLinux联盟 finally hHKLinux联盟 ...{ hHKLinux联盟 myConnection.Close(); hHKLinux联盟 } hHKLinux联盟 } hHKLinux联盟 } hHKLinux联盟 hHKLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|