关于using不解,求科普
private void button1_Click(object sender, EventArgs e)
{
string region = textBox1.Text.ToString();
string name = textBox2.Text.ToString();
string cs = "";
string queryString = "";
if (((Button)(sender)).Text == "Insert Northwind")
{
cs = "server=localhost\\sqlexpress;database=Northwind;integrated security=SSPI;";
queryString = "insert into mytable (myregion,myname) values(N' " + region + "',N'" + name + "' )";
//queryString = "INSERT INTO MyTable (MyRegion, MyName) VALUES(N'火', N'風')";
}
else if (((Button)(sender)).Text == "Insert ADONETDB")
{
cs = "server=localhost\\sqlexpress;database=ADONETDB;integrated security=SSPI;";
queryString = "insert into dept (deptname,note) values('" + region + "','" + name + "' )";
}
//1.引用SqlConnection物件連接資料庫
using (SqlConnection cn = new SqlConnection(cs))
{
//2.開啟資料庫
cn.Open();
//3.引用SqlCommand物件
using (SqlCommand cmd = new SqlCommand(queryString, cn))
{
//4.執行SQL語法
cmd.ExecuteNonQuery();
}
}
}
这里的using是什么意思,定义一个范围,在范围结束时处理对象的话这个sqlconnection对象