通过类返回SqlDataReader类型对象, 不能在类库中关闭SqlDataReader. 这样在调用的时候就会出错.

一般返回dataset或者datatable就好了,因为它们是离线的数据集 而reader你要控制连接的关闭,类里先关了用不了,调用的时候再关又不好。

如果一定要用的话:这样用:

    con.Open();
    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//当关闭DataReader时自动关闭Connection
   
// con.Close();不能关闭
    return dr;

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2022-01-06
  • 2022-01-11
  • 2021-08-18
  • 2022-12-23
相关资源
相似解决方案