2017-03-08 30 views
0

我有這個代碼插入數據到數據庫。在我提交之前,我想確保數據輸入成功,然後提交其他回滾兩個插入。SQLconnection不支持並行事務

try { 
    statement one commit; 
    statement two commit; 
} 
catch { 
    error message 
    statement 1 Rollback(); 
    statement 2 Rollback(); 
} 
finally { 
    if (con.State == Con.Open) 
     con.Close(); 
} 
+2

你的問題有點不清楚,標題混淆爲1)你沒有在psuedocode中創建一個數據庫事務,並且2)這些語句沒有並行運行。 – stuartd

+0

將所有插入腳本放入try {}中,因爲如果嘗試失敗,它們不會並行運行,它只會轉到'catch',您基本上會說「嘗試插入此數據,但如果失敗,則抓住它」。 –

+0

也許看看[在.net中的事務](http://stackoverflow.com/questions/224689/transactions-in-net) – stuartd

回答

0

你是非常接近做到這一點,你只要把一個commit聲明兩種說法後,如果代碼達到這一點,這意味着的任何沒有拋出異常,所以你可以執行提交,因爲您確定這兩個語句已成功執行。

try 
{ 
    statement one; 
    statement two; 
    commit; 
} 
catch 
{ 
    error message 
    statement 1 Rollback(); 
    statement 2 Rollback(); 
} 
finally 
{ 
    if (con.State == Con.Open) 
      con.Close(); 
}