宣告
private SqlConnection ConnectionToSql =
 new SqlConnection("Data Source=.\\sqlexpress;
Initial Catalog=Northwind;Integrated
Security=True");

ConnectionToSql.StateChange += new
System.Data.StateChangeEventHandler(this.ConnectionToSql_StateChange);

ConnectionToSql.Open();

//DoSomething

//當呼叫Close()時,未認可的交易會被rolled back
ConnectionToSql.Close();

當連線的狀態改變時,會觸發

private void ConnectionToSql_StateChange(object sender,
StateChangeEventArgs e)
{
   myLabel.Text = e.CurrentState.ToString();
}

另外下面這行可以切換資料庫

ConnectionToSql.ChangeDatabase("welkin");

 

welkingunther 發表在 痞客邦 PIXNET 留言(0) 人氣()