using 語句的用法
阿新 • • 發佈:2019-02-05
using (Font font3 = new Font("Arial", 10.0f), font4 = new Font("Arial", 10.0f)) { // Use font3 and font4. }
我是這樣理解的在Page_Load下連線SQL Server 2005
string connectionString = "server=localhost;database=Northwind;integrated security=SSPI";
SqlConnection mySqlConnection = new SqlConnection(connectionString);
using (mySqlConnection)
{
mySqlConnection.Open();
lblInfo.Text = "<b>mySqlConnection物件的ConnectionString屬性為:<b>" +
mySqlConnection.ConnectionString + "<br>";
lblInfo.Text += "<b>mySqlConnection物件的ConnectionTimeout屬性為<b>" +
mySqlConnection.ConnectionTimeout + "<br>";
lblInfo.Text += "<b>mySqlConnection物件的Database屬性為<b>" +
mySqlConnection.Database + "<br>";
lblInfo.Text += "<b>mySqlConnection物件的DataSource屬性為<b>" +
mySqlConnection.DataSource + "<br>";
lblInfo.Text += "<b>mySqlConnection物件的ServerVersion屬性為<b>" +
mySqlConnection.ServerVersion + "<br>";
lblInfo.Text += "<b>mySqlConnection物件的當前狀態為<b>"+
mySqlConnection.State + "<br>";
}
lblInfo.Text += "<br><b>關閉連線後的mySqlConnection物件的狀態為:</b>";
lblInfo.Text += mySqlConnection.State.ToString();
這樣就不用寫
try{}
catch(Exception e){}
finally{}
顯示的關閉資料連線 在超過using的範圍會自動關閉