Ado.Net间歇性问题的一种解决办法

      最近项目上遇到一个很奇怪的问题,查询经常性的出错,这种出错是间歇性的。比如在代码中写select column1 from talbe1。执行完毕后Fill一个DataSet,再访问DataSet中的TableRow以及Column。通常会有一些莫名其妙的错误,例如
    Can not find Table 0.
    Column "column1" dose not belong to Table table.

   
在大多数情况下,没有任何问题,但是程序运行一会儿(不定期),就会出现这个问题。过一会儿可能也好了,但是几十分钟或几个小时后又出现了。
   
经过种种分析、排除,查找原因,最后把问题确定在connection.Close()的上。

SqlConnection.Close Method

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlconnectionclassclosetopic.asp

CAUTION   Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. In a finalizer, you should only releaseunmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. For more information, see Programming for Garbage Collection.


根据这个文章,我们注释了log(日志记录)处的Finalize中的connection的close方法。问题得以解决,但是其原理百思不得其解。

原文地址:https://www.cnblogs.com/Sniper/p/343986.html