有没有办法在开始修改该记录之前锁定一条记录?

时间:2017-04-09 18:54:06

标签: c# ado.net firebird firebird-embedded

在开始修改该记录之前,有没有办法锁定一条记录?

string stOpenConn = new FbConnectionStringBuilder {
    Database = stPathFilename,
    UserID = stUserID,
    Password = stPassword,
    ServerType = FbServerType.Embedded,
    ClientLibrary = stCLIENT_LIBRARY
}.ToString();

try {
    using(FbConnection fbConn = new FbConnection(stOpenConn)) {
        fbConn.Open();

        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();
        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();
        }//using
    }//using
}//try

然后在修改完成后释放?

我假设记录锁定在连接打开后...在BeginTransaction之前...并在fbTransaction.Commit之后释放记录锁定。

1 个答案:

答案 0 :(得分:1)

根据您的想法,您可以(小心)使用&#34; WITH LOCK&#34;选择的一个子句,它将变成一个悲观的锁(这就是你在使用它时需要谨慎的原因)。有关更多信息,请访问https://firebirdsql.org/refdocs/langrefupd21-notes-withlock.html A&#34;哑巴&#34;更新也将成为一种悲观的锁定。