How NOCOUNT affects ADO.NET一文中提到:

Previously, in Classic ASP and ADO, NOCOUNT was most commonly a factor if you were checking RecordsAffected on Command.Execute(). SqlDataReader does have a RecordsAffected property, but it's rarely used since it's not populated until the datareader has iterated all the rows and the datareader is closed. There are some possible implications if you're using DataAdapters to submit changes to the database, since it uses the rows affected result to determine if the update succeeded. Probably the easiest way to check for that case is to search the codebase for SqlDataAdapter and see if the Update() method is being called.

还有:

Tech notes if you're interested in tracing how RecordsAffected is set and used by pointing Reflector at System.Data:
System.Data.SqlClient.TdsParser.ProcessDone() sets RecordsAffected.
System.Data.Common.DbDataAdapter.UpdateRow() uses reader.RecordsAffected in determining whether to call AcceptChanges and ApplyToDataRow.

保险一点来说,通过作者所说的调用Update()来做判断似乎并不可靠,中间有很多步骤都有可能出问题从而影响最终的结果。同时,debug起来的也有麻烦了,嵌入sql debug后对于调试程序很有用,而如果按作者所说这个debug的功能都被削减了。又把database和application给分开调了。

评论中有人写了一个简单的测试程序,并给出了他的测试结果,他认为还是SET NOCOUNT ON快。

测试使用的存储过程


我与他测试的环境不同,在xp + SQL 2005 Ent with SP1上测试的,结果结论相反,把结果数据也share给大家:

NoCountOff   |   NoCountOn | xRate

7106    5893    20.58%
6156    6890    -10.61%
7093    7343    -3.41%
5780    6406    -9.77%
5563    6860    -18.91%
5610    7390    -24.09%
5810    6923    -21.38%
6296    7436    -15.33%

-- insert 1
46    0
16    0
0    0
0    0
16    0

-- insert 1000
420     393     6.88%
423     390     8.47%
966     423     128.37%

-- insert 100,000
43250    42860    0.91%

-- delete all then insert 100,000
42076    39826    5.65%

如果您也亲自测试一下可以很明显的发现:结果对于证明起到的作用很小。

posted on 2006-09-05 16:19  维生素C.NET  阅读(2199)  评论(0编辑  收藏  举报