using(...)
{........}
定義了一個范圍,等范圍結束以后進行資源的釋放。
例如:
using(SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User ID=sa;Password=sa")) { conn.open(); using(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select count(*) from studentInfo"; cmd.ExecuteScalar(); } }
這樣,當運行完成后就會釋放使用的數據庫資源,減輕數據庫的負擔。