有沒有一種方法可以更好地優化此代碼,而不使用TransactionScope?在不使用分佈式事務的情況下優化此代碼
public static bool DeleteItem(int cusID)
{
int result = 0;
using (OleDbConnection myConnection = new OleDbConnection(AppConfiguration.ConnectionString))
{
string delOrders= "DELETE FROM ORDERS WHERE cusID=?";
string delCustomer= "DELETE FROM CUSTOMERS WHERE cusID=?";
OleDbCommand myCommand = null;
OleDbTransaction tran = null;
try
{
myConnection.Open();
tran=myConnection.BeginTransaction();
using (myCommand = new OleDbCommand(delOrders, myConnection))
{
myCommand.Parameters.Add("cusID", cusID);
myCommand.Transaction = tran;
myCommand.ExecuteNonQuery();
}
using (myCommand = new OleDbCommand(delCompany, myConnection))
{
myCommand.Parameters.Add("cusID", cusID);
myCommand.Transaction = tran;
myCommand.ExecuteNonQuery();
}
tran.Commit();
result = 1;
}
catch (OleDbException ex)
{
tran.Rollback();
throw ex;
}
finally
{
myConnection.Close();
}
}
return (result>0);
}
爲什麼?它出什麼問題了?它慢嗎? – Justin 2011-04-19 06:34:15
FWD:[3參數形式](http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.oledbcommand%28v=VS.90%29.aspx)的OleDbCommand ctor允許指定交易。我也更喜歡'使用'。 – 2011-04-19 06:36:25
感謝pst。我剛剛被要求優化這個代碼,你可以幫助我處理交易的使用塊嗎? – 2011-04-19 06:47:50