日期:2014-05-18  浏览次数:20403 次

怎样释放DATAREADER的资源呢??
protected void gvxgclinfo_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  { //设置图书类型
  string cl_district = e.Row.Cells[12].Text; //获取图书类型编号
  string typeSql = "select * from zxd_cl_district where district_id=" + cl_district;
  OracleDataReader typeSdr = dataOperate.getRow(typeSql);
  typeSdr.Read(); //读取一条记录
  e.Row.Cells[12].Text = typeSdr["district_name"].ToString();
  }
  }
注:getrow如下
public static OracleDataReader getRow(string sql)
  {
  try
  {
  OracleConnection con = createCon();
  con.Open();
  OracleCommand com = new OracleCommand(sql, con);
  return com.ExecuteReader();
  }
  catch
  {
  return null;
  }
  finally
  { }
  } 


------解决方案--------------------
e.Row.Cells[12].Text = typeSdr["district_name"].ToString();
typeSdr.Dispose();
------解决方案--------------------
.Dispose();
------解决方案--------------------
LZ在使用datareader的时候需要注意下,在或者datareader的方法里面不能关闭连接,不然得到的datareader将无法使用,但是你如果不关闭连接的话在使用datareader后将它释放掉,此时连接也不算关闭的,所以建议datareader的写法为
C# code

public static OracleDataReader getRow(string sql)
{
  try
  {
  OracleConnection con = createCon();
  con.Open();
  OracleCommand com = new OracleCommand(sql, con);
  return com.ExecuteReader([color=#FF0000]CommandBehavior.CloseConnection[/color]);
  }//在获取datareader的时候加了红色的部分后,将datareader释放时将同时断开连接
  catch
  {
  return null;
  }
  finally
  { }
}

//使用datareader
using(OracleDataReader odr=getRow(sql))
{
  //你的方法
}