存储过程,分页查询

[color=red][b]存储过程[/b][/color]

Create PROCEDURE [dbo].[ProQuery]
-- Add the parameters for the stored procedure here
@Tables varchar(1000), --表名称,视图
@PrimaryKey varchar(100), --主关键字
@Sort varchar(200) = NULL, --排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc
@CurrentPage int = 1, --当前页码
@PageSize int = 10, --分页尺寸
@Fields varchar(1000) = '*',
@Filter varchar(1000) = NULL, --过滤语句,不带Where
@Group varchar(1000) = NULL --Group语句,不带Group By

AS
/**//*默认排序*/
IF @Sort IS NULL or @Sort = ''
SET @Sort = @PrimaryKey
DECLARE @SortTable varchar(100)
DECLARE @SortName varchar(100)
DECLARE @strSortColumn varchar(200)
DECLARE @operator char(2)
DECLARE @type varchar(100)
DECLARE @prec int
/**//*设定排序语句.*/
IF CHARINDEX('DESC',@Sort)>0
BEGIN
SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
SET @operator = '<='
END
ELSE
BEGIN
IF CHARINDEX('ASC', @Sort) = 0
SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
SET @operator = '>='
END
IF CHARINDEX('.', @strSortColumn) > 0
BEGIN
SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
END
ELSE
BEGIN
SET @SortTable = @Tables
SET @SortName = @strSortColumn
END
Select @type=t.name, @prec=c.prec
FROM sysobjects o
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
Where o.name = @SortTable AND c.name = @SortName
IF CHARINDEX('char', @type) > 0
SET @type = @type + '(' + CAST(@prec AS varchar) + ')'
DECLARE @strPageSize varchar(50)
DECLARE @strStartRow varchar(50)
DECLARE @strFilter varchar(1000)
DECLARE @strSimpleFilter varchar(1000)
DECLARE @strGroup varchar(1000)
/**//*默认当前页*/
IF @CurrentPage < 1
SET @CurrentPage = 1
/**//*设置分页参数.*/
SET @strPageSize = CAST(@PageSize AS varchar(50))
SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(50))
/**//*筛选以及分组语句.*/

IF @Filter IS NOT NULL AND @Filter != ''
BEGIN
SET @strFilter = ' Where ' + @Filter + ' '
SET @strSimpleFilter = ' AND ' + @Filter + ' '
END
ELSE
BEGIN
SET @strSimpleFilter = ''
SET @strFilter = ''
END
IF @Group IS NOT NULL AND @Group != ''
SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
SET @strGroup = ''
/**//*执行查询语句*/
EXEC(
'
DECLARE @SortColumn ' + @type + '
SET ROWCOUNT ' + @strStartRow + '
Select @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' orDER BY ' + @Sort + '
SET ROWCOUNT ' + @strPageSize + '
Select ' + @Fields + ' FROM ' + @Tables + ' Where ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' orDER BY ' + @Sort + '
'
)


[color=red][b]C#代码[/b][/color]

private void TestQuery()
{
SqlDataAdapter ads = new SqlDataAdapter();
string connstr = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(connstr);
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand("ProQuery", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Tables", SqlDbType.VarChar);
cmd.Parameters["@Tables"].Value = "Customer_Company_Info";

cmd.Parameters.Add("@PrimaryKey", SqlDbType.VarChar);
cmd.Parameters["@PrimaryKey"].Value = "Company_Tax_Num";

cmd.Parameters.Add("@CurrentPage", SqlDbType.Int);
cmd.Parameters["@CurrentPage"].Value = 1;

cmd.Parameters.Add("@PageSize", SqlDbType.Int);
cmd.Parameters["@PageSize"].Value = 1;

cmd.Parameters.Add("@Filter", SqlDbType.VarChar);
cmd.Parameters["@Filter"].Value = "Security_Fiscal=1";

cmd.ExecuteNonQuery();
ads.SelectCommand = cmd;

DataSet ds = new DataSet();
ads.Fill(ds);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值