天天看點

commandbehavior.closeconnetion 的作用及了解

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;  
  }