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());
}
{
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; }