关于ExecuteReader()获得参数值和返回值的方法

本来说不在写博客,可是老空着有点不大好意思,所以就花点时间贴点东西。

在ADO.NET里面,我往往是通过ExecuteReader()得到SqlDataReader,然后再将其邦定到Reapter控件中,这样以最高效率读取数据,并高效邦定数据。在最开始使用SQL2005进行开发时,就遇到过这样一个问题:在邦定控件前需要读出数据总量,而使用SqlDataCommand的ExecuteReader方法并没有获得参数值和返回值得方法。具体代码可以参考如下:

假如我们现在有张表Article,里面有id和title两个字段,共条数据。我们的存储过程名称是LoadArticles,代码如下:

CREATE PROC LoadArticles @totalArticles int output
AS
SET NOCOUNT ON;
BEGIN
    SELECT @totalArticles = COUNT(id) FROM Article
    SELECT id, title FROM Article
END

我们现在来执行以下ADO.NET程序:

SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Blog");
conn.Open();
SqlCommand command = new SqlCommand("LoadArticles", conn);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@totalArticles", SqlDbType.Int));
command.Parameters["@totalArticles"].Direction = ParameterDirection.Output;

SqlDataReader reader = command.ExecuteReader();

Console.WriteLine(command.Parameters["@totalArticles"].Value); // Output: 0;

return reader;

如果是新手,那么一定会对这段代码没什么感觉,因为一般人估计都没有用过这段代码,甚至根本没有考虑过这个事情。但是什么时候我们最需要它呢?分页!那么普通的开发人估计还是没有想过,因为一般情况下人们都酷爱使用GridView。而GridView恰恰是使用之大忌,虽然使用方便,但是对性能的损伤也是大的可以。同时大部分都是使用的DataSet,而不是SqlDataReader,因而上述情况也不会发生。那么对于追求高效的人来说,Repeater是经典也是首选控件,SqlDataReader也将是首选数据源,那么如果有很多数据的时候,为了能够分页,那么唯一必要的数据就是数据总量,才有可能遇到上述问题。有人说我可以另外来个存储过程,得到相应的数据总量就可以了,对的,但是这样会对设计模式有些影响,并且代码也会变得更多。好的做法还是将其放到一个存储过程里面。

那么上面的代码为什么会输出为零呢?错不在代码本身,而在没有很好的把握SqlDataReader,在没有释放SqlDataReader之前,是不能获得参数值的,因为SqlDataReader中的数据是在随邦定过程中读取的。下面我们换段代码看看:

SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Blog");
conn.Open();
SqlCommand command = new SqlCommand("LoadArticles", conn);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@totalArticles", SqlDbType.Int));
command.Parameters["@totalArticles"].Direction = ParameterDirection.Output;

SqlDataReader reader = command.ExecuteReader();
reader.Dispose();

Console.WriteLine(command.Parameters["@totalArticles"].Value); // Output: 10;

return reader;

哦,这段代码正确,验证了我上面说的理论。但是事实上,我们在设计软件的时候,遇到的问题是在还没有进行数据邦定前,数据是不能销毁的,同时为顾及设计模式的问题,我们又该如何是好?首先更改我们的存储过程

ALTER PROC LoadArticles
AS
SET NOCOUNT ON;
BEGIN
    SELECT COUNT(id) FROM Article
    SELECT id, title FROM Article
END

然后更改我们的数据访问代码:

SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Blog");
conn.Open();
SqlCommand command = new SqlCommand("LoadArticles", conn);
command.CommandType = CommandType.StoredProcedure;

SqlDataReader reader = command.ExecuteReader();
if(reader.Read())
    Console.WriteLine(reader.GetInt32(0)); // Output: 10;

reader.NextResult();
return reader;

呵呵,怎么样?我们就数据总量当作一条数据选择,在SqlDataReader中读取出来,然后将数据集移向下一个数据集,以此来达到既得到数据总量,又能在进行数据邦定之后再对SqlDataReader进行销毁,重要的是可以不必改变设计模式上的变动  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值