c#调用存储过程,并返回数据集

阿狸Ahri 2013-12-29 06:50:07

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "JSON";
int index =Convert.ToInt32( context.Request["pageIndex"]);
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=NetShop;Integrated Security=True");
con.Open();
string sql = string.Format("select * from NS_Shop_Discuss");
string namepro = "xp_GetPage ";
SqlParameter apa1 = new SqlParameter("@sql",SqlDbType.VarChar,255);
apa1.Value = sql;
SqlParameter apa2 = new SqlParameter("@page",SqlDbType.Int,64);
apa2.Value = index;
SqlParameter apa3 = new SqlParameter("@pageSize", SqlDbType.Int, 64);
apa3.Value = 5;
SqlParameter apa4= new SqlParameter("@totalcount", SqlDbType.Int, 64);
apa4.Direction = ParameterDirection.Output;
SqlCommand com = new SqlCommand(namepro, con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add(apa1);
com.Parameters.Add(apa2);
com.Parameters.Add(apa3);
com.Parameters.Add(apa4);
SqlDataAdapter ad=new SqlDataAdapter();
ad.SelectCommand = com;
DataSet da = new DataSet();
ad.Fill(da);
DataTable table = new DataTable();
table = da.Tables[0];
string json = CreateJsonParameters(table);

context.Response.Write(json);
}



CREATE PROCEDURE xp_GetPage
(
@sql varchar(1000),
@page int = 1,
@pageSize int = 20
@totalcount int
)
AS
SET NOCOUNT ON
DECLARE @P1 int --P1是游标的id

SET @page = (@page-1) * @pagesize + 1
EXEC sp_cursoropen @P1 output,@sql
EXEC sp_cursorfetch @P1, 16, @page, @pagesize
EXEC sp_cursorclose @P1

--上面的就够了,下面的代码是为了统计总记录条数
select @totalCount=@@RowCount


GO




为什么单步运行发现DataSet里面没值的? 可是点查看dataset里面的table[0]能查到表头,我数据库有数据的
...全文
326 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
卧_槽 2013-12-30
  • 打赏
  • 举报
回复
或者你的表里面只有5条数据。
卧_槽 2013-12-30
  • 打赏
  • 举报
回复
sp_cursoropen 贴出来看看
lutaotony 2013-12-30
  • 打赏
  • 举报
回复
先查看连接字符串是否正确。 再查看参数是否正确。 最后把参数带到存储过程查看是否正确。
阿狸Ahri 2013-12-30
  • 打赏
  • 举报
回复
解决了 应该在第二章表 Table[1]
  • 打赏
  • 举报
回复
如果返回有列名的空数据集,说明调用存储过程应该是成功的。你看你数据库连接字符串对不对。
  • 打赏
  • 举报
回复
截图上看不出DataSet里面没值

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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