C#操作MSSQL数据库通用类

 

上面用代码中利用存储过程分页中使用到得存储过程“getdataset"如下:


ALTER proc [dbo].[GetDataSet]
@TableList Varchar(500)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
@TableName Varchar(100), --搜索的表名
@SelectWhere Varchar(500),--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20),--表主键字段名。比如:id
@SelectOrder Varchar(500)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
@intPageNo int=1, --页号
@intPageSize int=10 ,--每页显示数
@RecordCount int OUTPUT   --总记录数(存储过程输出参数)
as 

declare @TmpSelect    NVarchar(600) 
declare @Tmp     NVarchar(600) 

set nocount on--关闭计数

set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere

execute sp_executesql
@TmpSelect, --执行上面的sql语句
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@RecordCount   OUTPUT

   --if (@RecordCount = 0) --如果没有贴子,则返回零
   --return 0
  
/*判断页数是否正确*/
   if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
    return (-1)
set nocount off--打开计数
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' '+@SelectWhere+' and '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值