亲爱的前辈们,我是一名刚工作不久的asp.net程序员,我现在有问题需请教大家!
我用了一个分页的存储过程,需要查出结果集,以及输出参数(总条数),但是现在总条数一直返回不了,不知道怎么回事,我现在快急死了,呜呜!请求大家帮帮忙,我把代码贴出来
存储过程
:create proc Proc_ProductClass_Test
-- 获取指定页的数据 @tblName varchar(255)='tb_ProductClass', -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(1000)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
@RowCount int = 1 output -- 返回记录总数AS
declare @strSQL nvarchar(4000) -- 主语句
declare @strTmp nvarchar(110) -- 临时变量
declare @strOrder nvarchar(400) -- 排序类型 if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc '
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc '
end if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName + '] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
--计算总条数
set @strSQL = @strSQL + ' select @RowCount = count(*) from [' + @tblName + ']'
if (len(@strWhere)>0)
set @strSQL = @strSQL + ' where '+@strWhere exec sp_executesql @strSQL, N'@RowCount int OutPut',@RowCount OutPut
print @RowCountGO程序代码:
public static IList<ProductClassInfo> GetTestList(PageInfo info)
{
IList<ProductClassInfo> ProClassList = new List<ProductClassInfo>();
ProductClassInfo Pinfo = null;
SqlParameter[] parameters = SqlHelper.GetCachedParameters("Proc_ProductClass_Test");
if (parameters == null)
{
parameters = new SqlParameter[]
{
new SqlParameter("@tblName",SqlDbType.VarChar,255),
new SqlParameter("@strGetFields",SqlDbType.VarChar,1000),
new SqlParameter("@fldName",SqlDbType.VarChar,1000),
new SqlParameter("@PageSize",SqlDbType.Int,4),
new SqlParameter("@PageIndex",SqlDbType.Int,4),
new SqlParameter("@OrderType",SqlDbType.Bit,2),
new SqlParameter("@strWhere",SqlDbType.VarChar,1500),
new SqlParameter("@RowCount",SqlDbType.Int,4)
}; SqlHelper.CacheParameters("Proc_ProductClass_Test", parameters);
}
parameters[0].Value = info.TblName;
parameters[1].Value = "*";
parameters[2].Value = "dt_AddTime";
parameters[3].Value = 2;
parameters[4].Value = 1;
parameters[5].Value = true;
parameters[6].Value = "";
parameters[7].Direction = ParameterDirection.Output; using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.StoredProcedure, "Proc_ProductClass_Test",parameters))
{
while (reader.Read())
{
Pinfo = new ProductClassInfo();
Pinfo.vc_ClassName = reader["vc_ClassName"].ToString();
ProClassList.Add(Pinfo);
}
info.TotalCount = Convert.ToInt32(parameters[parameters.Length - 1].Value);
}
return ProClassList;
}希望大家能够尽快帮我解决!!!!!!!!!!!
我用了一个分页的存储过程,需要查出结果集,以及输出参数(总条数),但是现在总条数一直返回不了,不知道怎么回事,我现在快急死了,呜呜!请求大家帮帮忙,我把代码贴出来
存储过程
:create proc Proc_ProductClass_Test
-- 获取指定页的数据 @tblName varchar(255)='tb_ProductClass', -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(1000)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
@RowCount int = 1 output -- 返回记录总数AS
declare @strSQL nvarchar(4000) -- 主语句
declare @strTmp nvarchar(110) -- 临时变量
declare @strOrder nvarchar(400) -- 排序类型 if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc '
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc '
end if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName + '] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
--计算总条数
set @strSQL = @strSQL + ' select @RowCount = count(*) from [' + @tblName + ']'
if (len(@strWhere)>0)
set @strSQL = @strSQL + ' where '+@strWhere exec sp_executesql @strSQL, N'@RowCount int OutPut',@RowCount OutPut
print @RowCountGO程序代码:
public static IList<ProductClassInfo> GetTestList(PageInfo info)
{
IList<ProductClassInfo> ProClassList = new List<ProductClassInfo>();
ProductClassInfo Pinfo = null;
SqlParameter[] parameters = SqlHelper.GetCachedParameters("Proc_ProductClass_Test");
if (parameters == null)
{
parameters = new SqlParameter[]
{
new SqlParameter("@tblName",SqlDbType.VarChar,255),
new SqlParameter("@strGetFields",SqlDbType.VarChar,1000),
new SqlParameter("@fldName",SqlDbType.VarChar,1000),
new SqlParameter("@PageSize",SqlDbType.Int,4),
new SqlParameter("@PageIndex",SqlDbType.Int,4),
new SqlParameter("@OrderType",SqlDbType.Bit,2),
new SqlParameter("@strWhere",SqlDbType.VarChar,1500),
new SqlParameter("@RowCount",SqlDbType.Int,4)
}; SqlHelper.CacheParameters("Proc_ProductClass_Test", parameters);
}
parameters[0].Value = info.TblName;
parameters[1].Value = "*";
parameters[2].Value = "dt_AddTime";
parameters[3].Value = 2;
parameters[4].Value = 1;
parameters[5].Value = true;
parameters[6].Value = "";
parameters[7].Direction = ParameterDirection.Output; using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.StoredProcedure, "Proc_ProductClass_Test",parameters))
{
while (reader.Read())
{
Pinfo = new ProductClassInfo();
Pinfo.vc_ClassName = reader["vc_ClassName"].ToString();
ProClassList.Add(Pinfo);
}
info.TotalCount = Convert.ToInt32(parameters[parameters.Length - 1].Value);
}
return ProClassList;
}希望大家能够尽快帮我解决!!!!!!!!!!!
存储过程我不常用帮不到你哦`
AspNetpager第三方控件也能分页
gridview自带分页
这么多办法你干嘛要弄这个呢
这个获得的一直都为0,根本获得不到Print的结果,郁闷吧!