如何使用FbTransactionOptions / TransactionBehavior实现Wait?
我找到了一些关于使用FbTransactionOptions / TransactionBehavior的信息,但没有足够的细节来实现。
string stCmd = "UPDATE " + stTableName + " SET " + liststFieldNamesNoKeyID[0] + " = @p0";
for (int iii = 1; iii < liststFieldNamesNoKeyID.Count(); iii++)
stCmd += ", " + liststFieldNamesNoKeyID[iii] + " = @p" + iii.ToString();
stCmd += " WHERE" + stFieldKeyID + "= @p" + liststFieldNamesNoKeyID.Count().ToString();
FbTransaction fbTransaction = fbConn.BeginTransaction();
new FbTransactionOptions()
{
TransactionBehavior = FbTransactionBehavior.Concurrency |
FbTransactionBehavior.Wait
};
using (FbCommand fbCmd = new FbCommand(stCmd, fbConn, fbTransaction)) {
for (int iii = 0; iii < liststFieldNamesNoKeyID.Count(); iii++) {
string stPlaceHolder = "@p" + (iii).ToString();
string stValue = liststNewValuesNoKeyID[iii];
fbCmd.Parameters.AddWithValue(stPlaceHolder, stValue);
}
int iKeyID = Convert.ToInt32(stKeyID);
fbCmd.Parameters.AddWithValue("@p" + liststFieldNamesNoKeyID.Count().ToString(), iKeyID);
fbCmd.ExecuteNonQuery();
fbTransaction.Commit();
在上面的示例中,未使用TransactionBehavior。重新排列会导致编译器抱怨。
new FbTransactionOptions()
{
TransactionBehavior = FbTransactionBehavior.Concurrency |
FbTransactionBehavior.Wait
};
FbTransaction fbTransaction = fbConn.BeginTransaction(TransactionBehavior);
另外,如何设置Wait的值?
答案 0 :(得分:2)
您需要使用FbConnection.BeginTransaction(FbTransactionOptions options)
。你需要传递你创建的FbTransactionOptions
的实例,所以:
var transactionOptions = new FbTransactionOptions()
{
TransactionBehavior = FbTransactionBehavior.Concurrency |
FbTransactionBehavior.Wait
};
FbTransaction fbTransaction = fbConn.BeginTransaction(transactionOptions);
您的原始代码无效,因为您只创建了FbTransactionOptions
的实例并立即将其丢弃。第二次尝试不起作用,因为你没有TransactionBehavior
任何地方(它是FbTransactionOptions
的属性,而不是它自己的东西)。