难道的ExecuteScalar()有超过任何的ExecuteReader优势()? [英] Does ExecuteScalar() have any advantages over ExecuteReader()?

查看:107
本文介绍了难道的ExecuteScalar()有超过任何的ExecuteReader优势()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确实的ExecuteScalar()有超过的ExecuteReader任何优势()


< DIV CLASS =h2_lin>解决方案

的ExecuteScalar 只返回从数据集的第一行第一个值。内部它被视为就像的ExecuteReader(),一个的DataReader 打开时,该值回升和的DataReader 事后被销毁。我也一直想知道这种行为,但它有一个优点:它发生在框架内...你不能在速度方式的竞争框架



修改通过rwwilden:
拍摄用反射看看里面 SqlCommand.ExecuteScalar()你可以看到这几行:

  SqlDataReader的DS = this.RunExecuteReader(
CommandBehavior.Default,RunBehavior.ReturnImmediately,真正的的ExecuteScalar);
OBJ2 = this.CompleteExecuteScalar(DS,FALSE);



的ExecuteReader 究竟会发生什么。另一个优点是,的ExecuteScalar 收益当没有数据被读取。如果你使用的ExecuteReader ,你必须这样检查自己。


Does ExecuteScalar() have any advantages over ExecuteReader()?

解决方案

ExecuteScalar only returns the first value from the first row of the dataset. Internal it is treated just like ExecuteReader(), a DataReader is opened, the value is picked and the DataReader gets destroyed afterwards. I also always wondered about that behavior, but it has one advantage: It takes place within the Framework...and you can't compete with the Framework in manners of speed.

Edit By rwwilden: Taking a look with Reflector inside SqlCommand.ExecuteScalar() you can see these lines:

SqlDataReader ds = this.RunExecuteReader(
    CommandBehavior.Default, RunBehavior.ReturnImmediately, true, "ExecuteScalar");
obj2 = this.CompleteExecuteScalar(ds, false);

Exactly what happens inside ExecuteReader. Another advantage is that ExecuteScalar returns null when no data is read. If you use ExecuteReader, you'd have to check this yourself.

这篇关于难道的ExecuteScalar()有超过任何的ExecuteReader优势()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆