我们怎样选择使用ExecuteReader, ExecuteNonQuery, ExecuteScalar ...

SqlClient namespace in ADO.NET provides 3 basic methods to run queries with SqlCommand against MSSQL:


  • ExecuteReader
  • ExecuteNonQuery
  • ExecuteScalar

.NET Framework is around for 5 years already now, but these 3 methods are among the most misused in application development. The problem is that ExecuteReader will work for every query via SqlCommand, so most people use it everywhere. The direct result - slow and non-scalable solution. Other results include database bottlenecks, locks and connection leaks.


So I decided to provide a small cheat-sheet that clearly demonstrates when to use each method.


ExecuteReader

Do not use: when database query is going to provide for sure exactly 1 record. It may be getting record by its id (which is PK in the database) - GetOrderById and such. In this case use ExecuteNonQuery with output parameters.


Use: when database query is going to provide a set of records. It may be search or report.


ExecuteNonQuery

Use: when we are talking about a single database record - in Update, Insert, Delete and Get by Id. In all these cases we can use input/output/input-output parameters. Please note that from the application architecture point of view it is also good practices when your Insert and Update stored procedure returns changed record exactly like Get By Id method does.


ExecuteScalar

Do not use: when database query returns a single value and this value can be defined as parameter in T-SQL. ExecuteNonQuery with output parameter(s) is always preferred in this case since it is more flexible, tomorrow there will be 2 values therefore having ExecuteNonQuery we do not need to change method signatures.


Use: when database query returns a single value and this value cannot be defined as output parameter, because of T-SQL type limitation for variables. For example type image cannot be output parameter in MSSQL.


The most common example for ExecuteScalar is fetching a single image stored in the database and converting it to array of bytes. If you google it - most examples will demonstrate using of ExecuteReader to accomplish image handler, however ExecuteScalar will be more scalable and faster.


Conclusion

Always use ExecuteNonQuery except: when you have a set of records - use ExecuteReader and when you have a single output value that cannot be defined as a parameter - use ExecuteScalar. Hope this helped to clarify something. Enjoy :)

http://blogs.x2line.com/al/archive/2007/05/01/3049.aspx

posted @ 2008-03-10 13:30 许晓光 阅读(147) 评论(2)  编辑 收藏 网摘 所属分类: Database

  回复  引用  查看    
#1楼 2008-03-10 14:38 | Clark Zheng      
哦,又见E文,强迫自己精读之
  回复  引用  查看    
#2楼 [楼主]2008-03-10 15:02 | 许晓光      
呵呵, 俺E文差,还是原文吧! 

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-03-13 12:50 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接: