• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

在Delphi和.NET中实现分页功能的存储过程(返回多个记录集)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 
 
=============== SQL 中的存储过程 ====================
CREATE procedure splitPage
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int --每页行数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as pageCount,@rowcount as recCount,@currentpage as curPage
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off
GO

=============== Delphi 中应用存储过程 ===================
var myDataSet: _Recordset;
begin
ADODataSet1.Close ;
ADOStoredProc1.Close;
ADOStoredProc1.Parameters.ParamByName('@sqlstr').Value :='select * from History49'; //SQL
ADOStoredProc1.Parameters.ParamByName('@currentpage').Value := 2; //取得第2页
ADOStoredProc1.Parameters.ParamByName('@pagesize').Value := 20; //每页行数
ADOStoredProc1.ExecProc;
ADOStoredProc1.Open;

// 第 一个记录集
myDataSet := lhgDM.asProcLS.NextRecordset(0);
if myDataSet = nil then exit;

// 第 2 个记录集 (页数 ,记录数信息)
myDataSet := lhgDM.asProcLS.NextRecordset(1);
if myDataSet = nil then exit;
ADODataSet1.Recordset := myDataSet ;
ADODataSet1.First ;
pageCount := ADODataSet1.FieldByName('pageCount').AsInteger; // 总页数
recCount := ADODataSet1.FieldByName('recCount').AsInteger; //总记录数

// 第 3 个记录集 当前SQL请求第N页的记录
myDataSet := lhgDM.asProcLS.NextRecordset(2);
if myDataSet = nil then exit;
ADODataSet1.Recordset := myDataSet ;
ADODataSet1.first;
while not ADODataSet1.EOF do begin
...
end;
end;

=============== .NET(c#) 中应用存储过程 ===================
public DataView createDataViewWithPage(string strSQL,string dsName,int currentPage,
int pageRows,ref int pageCount,ref int RowCount)
{
if (currentPage<=0) return null;
if (SQLConn==null) SQLConn = setConn(SQLConnStr);
if (SQLConn==null) return null;

SqlCommand myCmd = new SqlCommand();
myCmd.CommandText = "splitPage" ; //存储过程名称
myCmd.CommandType = CommandType.StoredProcedure ;
myCmd.Parameters.Add("@sqlstr", SqlDbType.VarChar,4000).Value = strSQL;
myCmd.Parameters.Add("@currentpage", SqlDbType.Int).Value = currentPage;
myCmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = pageRows;
myCmd.Connection = SQLConn;

SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = myCmd;
DataSet ds = new DataSet();
sqlDA.Fill(ds,dsName);

pageCount = Convert.ToInt32(ds.Tables[1].DefaultView[0]["pageCount"].ToString()); //总有页数
RowCount = Convert.ToInt32(ds.Tables[1].DefaultView[0]["recCount"].ToString()); //总记录数
//curPage = Convert.ToInt32(ds.Tables[1].DefaultView[0]["curPage"].ToString()); //当前页

return ds.Tables[2].DefaultView;// ds.Tables[dsName].DefaultView ;
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap