using语句一般是用来释放内存的,但是以下句子
using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa**;database=NorthWind"))
{
DataTable dt_Country = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select distinct Country from employees", con);
sda.Fill(dt_Country);
cob_Country.DisplayMember = "Country";
cob_Country.ValueMember = "Country";
cob_Country.DataSource = dt_Country; cob_Country.DropDownStyle = ComboBoxStyle.DropDownList;
}
如何理解呢,特别是(SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa**;database=NorthWind"))
该如何理解
using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa**;database=NorthWind"))
{
DataTable dt_Country = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select distinct Country from employees", con);
sda.Fill(dt_Country);
cob_Country.DisplayMember = "Country";
cob_Country.ValueMember = "Country";
cob_Country.DataSource = dt_Country; cob_Country.DropDownStyle = ComboBoxStyle.DropDownList;
}
如何理解呢,特别是(SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa**;database=NorthWind"))
该如何理解
try
catch
finally
{
obj.Dispose()
}
对象执行完这一句后(cob_Country.DropDownStyle = ComboBoxStyle.DropDownList;) 关闭con对象释放资源
就是起到一个资源的有效释放~~
当在某个代码段中使用了类的实例,只要离开了这个代码段就自动调用这个类实例的Dispose。
参考
释放资源。 因为close()方法,我们经常忘记写,用using我们这样就不用写close()方法了