天使半只翼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
protected void bind()
{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("GetAllUser", conn);
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //CommandBehavior.CloseConnection使用方法
repeater1.DataSource = sdr;
repeater1.DataBind();
Response.Write(sdr.IsClosed.ToString()+"<br/>");
Response.Write(conn.State.ToString());

}


CommandBehavior.CloseConnection 它能够保证当SqlDataReader对象被关闭时,其依赖的连接也会被自动关闭。

 public static City getCity(int cID)  
  {  
  string sql = "select * from City where cID=" + cID;  
  SqlDataReader r = DBHress.GetReader(sql);  
  City city = null;
  if (r.Read())  
  {  
  city = new City();  
  city.CID = Convert.ToInt32(r["cID"]);  
  city.CName = (string)r["cName"];  
  }   
  r.Close();   // SqlDataReader 关闭时Connection 也相应关闭
  return city;  
  }  
posted on 2012-10-07 12:23  天使半只翼  阅读(203)  评论(0编辑  收藏  举报