c# oracle 分頁

來源:互聯網
上載者:User

//////////////////////////////////////////oracle部分預存程序

create or replace package PACK_PAGE is

 

 TYPE type_cur IS REF CURSOR;     --定義遊標變數用於返回記錄集
  PROCEDURE PROC_PAGE
  (
  Pindex in number,                --分頁索引
  Psql in varchar2,                --產生dataset的sql語句
  Psize in number,                 --頁面大小
  Pcount out number,               --返回分頁總數
  v_cur out type_cur               --返回當前頁資料記錄
  );
  procedure PROC_PAGECOUNT
  (
  Psqlcount in varchar2,           --產生dataset的sql語句
  Prcount   out number             --返回記錄總數
  );

end PACK_PAGE;

///////////////////////////////////////////////////////

create or replace package body PACK_PAGE is

 

 PROCEDURE PROC_PAGE
  (
  Pindex in number,
  Psql in varchar2,
  Psize in number,
  Pcount out number,
  v_cur out type_cur
  )
  AS
  v_sql VARCHAR2(5000);
  v_tempSql varchar2(5000);
  v_count number;
  v_Plow number;
  v_Phei number;
  Begin
  ------------------------------------------------------------取分頁總數
  v_sql := 'select count(*) from (' || Psql || ')';
  execute immediate v_sql into v_count;
  Pcount := ceil(v_count/Psize);
  ------------------------------------------------------------顯示任意頁內容
  v_Phei := (Pindex-1) * Psize + Psize;
  v_Plow := v_Phei - Psize + 1;
  v_tempSql := 'select rownum rn,t.* from ('|| Psql ||')t Where rownum <= ' || v_Phei ;            --要求必須包含rownum欄位
  v_sql := 'select * from (' || v_tempSql || ') where rn between ' || v_Plow || ' and ' || v_Phei ;
  open v_cur for v_sql;
  End PROC_PAGE;
 --**************************************************************************************
  procedure PROC_PAGECOUNT
  (
  Psqlcount in varchar2,
  Prcount   out number
  )
  as
  v_sql varchar2(5000);
  v_prcount number;
  begin
  v_sql := 'select count(*) from (' || Psqlcount || ')';
  execute immediate v_sql into v_prcount;
  Prcount := v_prcount;                  --返回記錄總數
  end PROC_PAGECOUNT;
 --**************************************************************************************
end PACK_PAGE;

 

////////////////////// c# 調用

   /// <summary>
        /// 分頁條件查詢
        /// </summary>
        /// <param name="sql">SQL語句</param>
        /// <param name="pageIndex">當前頁</param>
        /// <param name="pageSize">每頁包含的記錄數</param>
        /// <param name="tabName">填充資料集時的表名稱</param>
        /// <returns></returns>
        public DataSet QueryByPage(string sql, int pageIndex, int pageSize, string tabName)
        {
            using (OracleHelper orahp = new OracleHelper())
            {
                DataSet ds = new DataSet();
                ds.Tables.Add(new DataTable(tabName));

 

                orahp.Open();
                orahp.ExecuteSP_Page(ds.Tables[tabName], "pack_page.proc_page", sql, pageIndex, pageSize, true);
                orahp.Close();

                return ds;
            }
        }

 

 

   public int ExecuteSP_Page(DataTable outDataTable, string procName, string sql, int pageIndex, int pageSize, bool setCase)
        {
            if (null == connection)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

 

            int nReturn = 0;

            using (OracleCommand command = new OracleCommand(procName, connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.CommandTimeout = this._timeout;

                command.Parameters.Add("psql", OracleType.VarChar).Value = sql;
                command.Parameters.Add("pindex", OracleType.Number).Value = pageIndex;
                command.Parameters.Add("psize", OracleType.Number).Value = pageSize;
                command.Parameters.Add("pcount", OracleType.Number).Direction = ParameterDirection.Output;
                command.Parameters.Add("v_cur", OracleType.Cursor).Direction = ParameterDirection.Output;

                if (null != transaction)
                {
                    command.Transaction = transaction;
                }

                using (OracleDataAdapter oracAdapter = new OracleDataAdapter(command))
                {
                    if (setCase) outDataTable.CaseSensitive = true;
                    nReturn = oracAdapter.Fill(outDataTable);
                }

                command.Parameters.Clear();

                return nReturn;
            }
        }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.