难道的ExecuteScalar()有超过任何的ExecuteReader优势()? [英] Does ExecuteScalar() have any advantages over 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屋!