存储过程调不出数据。

罢懂 2010-07-22 05:33:12
分页类
public DataTable Pagination(string sqlStr, int currentpage, int pagesize)
{
conn = new SqlConnection(lt.Common.Config.connStr);
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "lt_Paginationcursor";
SqlParameter paramOUT = comm.Parameters.AddWithValue("@RETURN_VALUE", "");
paramOUT.Direction = ParameterDirection.ReturnValue;
SqlParameter paramOUTPUT = comm.Parameters.Add("@pagecount", SqlDbType.BigInt);
paramOUTPUT.Direction = ParameterDirection.Output;
SqlParameter paramOUTPUT1 = comm.Parameters.Add("@recordCount", SqlDbType.BigInt);
paramOUTPUT1.Direction = ParameterDirection.Output;
comm.Parameters.AddWithValue("sql", sqlStr);
comm.Parameters.AddWithValue("currentpage", currentpage);
comm.Parameters.AddWithValue("pagesize", pagesize);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dtable = new DataTable();
da.Fill(dtable);
_pageCount = System.Convert.ToInt32(comm.Parameters["@pagecount"].Value.ToString());
_recordCount = System.Convert.ToInt32(comm.Parameters["@recordcount"].Value.ToString());
return dtable;
}


存储过程 lt_Paginationcursor

ALTER PROCEDURE dbo.lt_Paginationcursor
@sql nvarchar(4000), --要执行的sql语句
@currentpage int=1, --要显示的页码
@pagesize int=10, --每页的大小
@pagecount int=0 output, --总页数
@recordCount int=0 output--总记录数
as
set nocount on
declare @cursor int --cursor 是游标的id
exec sp_cursoropen @cursor output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@pagecount output
select @recordCount=@pagecount
select @pagecount=ceiling(1.0*@pagecount/@pagesize) ,@currentpage=(@currentpage-1)*@pagesize+1
select @pagecount,@currentpage
exec sp_cursorfetch @cursor ,16,@currentpage,@pagesize
exec sp_cursorclose @cursor
return


调用执行

SQLServerDAL.Perform p = new SQLServerDAL.Perform();
DataTable dt = p.Pagination("select * from lt_mshospital", 1, 10);
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Write(p.pageCount + "/" + p.recordCount + "/");
Response.Write(dt.Rows.Count);


说明:数据库表中有61条数据,读出来后,无错误。
但是 总页数(p.pageCount)正确,总数据量(p.recordCount)正确。datatable没有数据,rows.count是0
为什么噢!~!~急死了。想不明白了。找不到原因了。。。
求高手帮帮忙吧!~~!
另外,存储过程我单独执行,是可以正常调出数据的。很正常。
就是和程序整合起来就读不出来了。。不知道为什么,求高手帮忙额!~
...全文
99 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
罢懂 2010-07-23
  • 打赏
  • 举报
回复
谢谢9楼。精准到位!~
telankes2000 2010-07-23
  • 打赏
  • 举报
回复
看你的存過過程 是調用的系統存儲過程
如果調用數據庫的自帶分頁存儲過程
那麼將返回2張表
第一張表只有表結構沒有數據
第二張表才有數據
也就是執行這句(exec sp_cursorfetch @cursor ,16,@currentpage,@pagesize )之後才會返回你想要的數據
從你寫的存儲過程來看 你要用的應該是第三張表
select @pagecount,@currentpage (這是第二張表)其實這句沒有必要 因為你已經通過out方式返回總記錄值了

public DataTable Pagination(string sqlStr, int currentpage, int pagesize)
{
conn = new SqlConnection(lt.Common.Config.connStr);
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "lt_Paginationcursor";
SqlParameter paramOUT = comm.Parameters.AddWithValue("@RETURN_VALUE", "");
paramOUT.Direction = ParameterDirection.ReturnValue;
SqlParameter paramOUTPUT = comm.Parameters.Add("@pagecount", SqlDbType.BigInt);
paramOUTPUT.Direction = ParameterDirection.Output;
SqlParameter paramOUTPUT1 = comm.Parameters.Add("@recordCount", SqlDbType.BigInt);
paramOUTPUT1.Direction = ParameterDirection.Output;
comm.Parameters.AddWithValue("sql", sqlStr);
comm.Parameters.AddWithValue("currentpage", currentpage);
comm.Parameters.AddWithValue("pagesize", pagesize);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dtable = new DataTable();
da.Fill(dtable);
_pageCount = System.Convert.ToInt32(comm.Parameters["@pagecount"].Value.ToString());
_recordCount = System.Convert.ToInt32(comm.Parameters["@recordcount"].Value.ToString());
return dtable;
}
========》
public DataTable Pagination(string sqlStr, int currentpage, int pagesize)
{
conn = new SqlConnection(lt.Common.Config.connStr);
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "lt_Paginationcursor";
SqlParameter paramOUT = comm.Parameters.AddWithValue("@RETURN_VALUE", "");
paramOUT.Direction = ParameterDirection.ReturnValue;
SqlParameter paramOUTPUT = comm.Parameters.Add("@pagecount", SqlDbType.BigInt);
paramOUTPUT.Direction = ParameterDirection.Output;
SqlParameter paramOUTPUT1 = comm.Parameters.Add("@recordCount", SqlDbType.BigInt);
paramOUTPUT1.Direction = ParameterDirection.Output;
comm.Parameters.AddWithValue("sql", sqlStr);
comm.Parameters.AddWithValue("currentpage", currentpage);
comm.Parameters.AddWithValue("pagesize", pagesize);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds= new DataSet ();
da.Fill(ds);
_pageCount = System.Convert.ToInt32(comm.Parameters["@pagecount"].Value.ToString());
_recordCount = System.Convert.ToInt32(comm.Parameters["@recordcount"].Value.ToString());
或者_pageCount = Convert.ToInt32(ds.Tables[1].Rows[0][0])
_recordCount = Convert.ToInt32(ds.Tables[1].Rows[0][1])
return ds.Tables[2];//返回第3張表
}




wanghuaide 2010-07-22
  • 打赏
  • 举报
回复
加个断点调试一下吧
wuyq11 2010-07-22
  • 打赏
  • 举报
回复
guyehanxinlei 2010-07-22
  • 打赏
  • 举报
回复
将参数带进去跟踪一下就知道了
lingrain 2010-07-22
  • 打赏
  • 举报
回复
分页存储过程我没用过呢。我都是直接在SQL语句里加入limint ?offset,?rows参数
在外面把PageIndex 和PageSize转换成offset,rows .
估计存储过程内部也是这么转换吧?
WORLDNIC 2010-07-22
  • 打赏
  • 举报
回复
ALTER PROCEDURE dbo.lt_Paginationcursor
@sql nvarchar(4000), --要执行的sql语句
@currentpage int=1, --要显示的页码
@pagesize int=10, --每页的大小
@pagecount int=0 output, --总页数
@recordCount int=0 output--总记录数
as
set nocount on
declare @cursor int --cursor 是游标的id
exec sp_cursoropen @cursor output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@pagecount output
set @recordCount=@pagecount
set @pagecount=ceiling(1.0*@pagecount/@pagesize) ,@currentpage=(@currentpage-1)*@pagesize+1

exec sp_cursorfetch @cursor ,16,@currentpage,@pagesize
exec sp_cursorclose @cursor
select @pagecount,@currentpage



myhope88 2010-07-22
  • 打赏
  • 举报
回复
路过,帮顶下
charlie_zhuo 2010-07-22
  • 打赏
  • 举报
回复
comm.Parameters.AddWithValue("sql", sqlStr);
comm.Parameters.AddWithValue("currentpage", currentpage);
comm.Parameters.AddWithValue("pagesize", pagesize);
// 加这句话看看
con.Open();
罢懂 2010-07-22
  • 打赏
  • 举报
回复
帮帮忙吧!~~!

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧