錯誤問題:閱讀器關閉時 FieldCount 的嘗試無效

 原因:
  使用了SqlDataReader來綁定數(shù)據后,將connection對象作了Close()處理
  類似

  public SqlDataReader   GetSomething()
  {
  conn.open();
  SqlDataReader   reader =
  sqlcmd.ExcecutReader(CommandBehavior.CloseConnection));
  conn.close();// occur error   here
  return   reader;
  }

  在綁定的時候調用了這個方法來指定數(shù)據源。

       如果使用這個方法則需要在調用函數(shù)中關閉Re

  ader這樣conn就可以自動關閉。

  如果是使用的是SqlDataAdapter和DataSet那么請去掉顯式關閉conn的調用。

       或者在finally中調用之。


原文鏈接:錯誤問題:閱讀器關閉時 FieldCount 的嘗試無效