錯(cuò)誤問題:閱讀器關(guān)閉時(shí) FieldCount 的嘗試無效

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

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

  在綁定的時(shí)候調(diào)用了這個(gè)方法來指定數(shù)據(jù)源。

       如果使用這個(gè)方法則需要在調(diào)用函數(shù)中關(guān)閉Re

  ader這樣conn就可以自動(dòng)關(guān)閉。

  如果是使用的是SqlDataAdapter和DataSet那么請(qǐng)去掉顯式關(guān)閉conn的調(diào)用。

       或者在finally中調(diào)用之。


原文鏈接:錯(cuò)誤問題:閱讀器關(guān)閉時(shí) FieldCount 的嘗試無效