//讓從資料庫抓回來的資料以XML的格式表現

SqlCommand myXMLCommand = new SqlCommand();
myXMLCommand.Connection = myConnection;
myXMLCommand.CommandType = CommandType.Text;
myXMLCommand.CommandText = "SELECT CustomerID FROM Customers For XML Auto";
myXMLCommand.Connection.Open();
System.Xml.XmlReader reader =myXMLCommand.ExecuteXmlReader();
reader.Close();
myXMLCommand.Connection.Close();

//非同步去執行Command,有以下幾個函式

BeginExecuteNonQuery
BeginExecuteReader
EndExecuteNonQuery==>在StatementComplete這個事件發生後去呼叫
EndExecuteReader==>在StatementComplete這個事件發生後去呼叫


//讓Command用交易的方式來執行

transaction = myConnection.BeginTransaction();
Command1.Transaction = transaction;
Command2.Transaction = transaction;
Command1.ExecuteNonQuery();
Command2.ExecuteNonQuery();

DialogResult response = MessageBox.Show(
"是否完成交易", "標題",
MessageBoxButtons.YesNo);


switch (response)
{
 case DialogResult.Yes:
 transaction.Commit();
 break;
 case DialogResult.No:
 transaction.Rollback();
 break;
}
myConnection.Close();

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