2011-08-25 62 views
0

我在C#中使用Firebird的dot net提供程序運行一些SQL命令。具體來說,我正在更改數據庫模式,並進行數據更新等。Firebird點網絡提供者沒有完全執行查詢?

作爲我的處理的一部分,我創建一個新表,運行查詢以從舊錶中複製數據,然後刪除舊錶。

當我做這個火鳥生成和錯誤:

unsuccessful metadata update object is in use

我已經做了一些看,它似乎查詢複製的數據沒有被「清除」我們什麼呢。我的意思是當我檢查Firebird中的監控表時,我的c#執行已暫停,我在MON$STATEMENTS表中看到查詢爲非活動狀態。這是我運行一個提交語句後。

我的問題:

有沒有辦法暫停,或等待,或強制查詢,全面完成之前,我嘗試運行下一個命令?

當我在ISQL中運行相同的查詢序列時,它可以很好地工作。有什麼不同的ISQL,我可以強制點網Firebird提供程序這樣做,它不會保持此查詢打開或什麼?

所以對於參考的代碼看起來是這樣的(顯然這是一個很簡單的):

// create the table 
    string commandString = "CREATE TABLE ..."; 

    // run the command in a transaction and commit it 
    mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable); 
    FbCommand command = new FbCommand(commandString, Connection, mtransaction); 
    command.ExecuteNonQuery(); 
    transaction.Commit(); 
    transaction.Dispose(); 
    transaction = null; 

    // copy the data to the new table from the old 
    commandString = "INSERT INTO ..."; 

    mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable); 
    FbCommand command = new FbCommand(commandString, Connection, mtransaction); 
    command.ExecuteNonQuery(); 
    transaction.Commit(); 
    transaction.Dispose(); 
    transaction = null; 

    // drop the old table 
    commandString = "DROP TABLE ..."; 

    mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable); 
    FbCommand command = new FbCommand(commandString, Connection, mtransaction); 
    command.ExecuteNonQuery(); 

    // this command fails with the exception 
    // if I pause execution in c# before running this command, and 
    // use isql to look at the db I see the new table, and the data fully populated 
    // and I also see the inactive insert command in MON$STATEMENTS 
    transaction.Commit(); 
    transaction.Dispose(); 
    transaction = null; 
+0

作爲參考,我使用火鳥2.1.0窗口建設,我使用我相信DDEXProvider-2.0.1的事情 – Beau

回答

0

好了,可怕的問題的解決方案:

我居然能得到這個由工作關閉和處理連接,然後重新連接。這導致「卡住」查詢以某種方式被移除,然後我可以執行表格放置命令。所以順序看起來像這樣:

// create the table 
string commandString = "CREATE TABLE ..."; 

// run the command in a transaction and commit it 
mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable); 
FbCommand command = new FbCommand(commandString, Connection, mtransaction); 
command.ExecuteNonQuery(); 
transaction.Commit(); 
transaction.Dispose(); 
transaction = null; 

// copy the data to the new table from the old 
commandString = "INSERT INTO ..."; 

mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable); 
FbCommand command = new FbCommand(commandString, Connection, mtransaction); 
command.ExecuteNonQuery(); 
transaction.Commit(); 
transaction.Dispose(); 
transaction = null; 


// ------------------ 
// Drop the connection entirely and start a new one 
// so the table can be dropped 

Connection.Close(); 
Connection.Dispose(); 

// build connection string 
FbConnectionStringBuilder csb = new FbConnectionStringBuilder(); 
csb.DataSource ... etc... 

// connect 
Connection = new FbConnection(connectionString); 
Connection.Open(); 

// Now have new connection that does not have weird 
// lingering query, and table can now be dropped 
// ----------------- 



// drop the old table 
commandString = "DROP TABLE ..."; 

mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable); 
FbCommand command = new FbCommand(commandString, Connection, mtransaction); 
command.ExecuteNonQuery(); 

// this no longer fails because the connection was complete closed 
// and re-opened 
transaction.Commit(); 
transaction.Dispose(); 
transaction = null; 

注意:我對此解決方案非常不滿意。它有效,但我不知道爲什麼。我不得不這樣做放棄一張桌子,這對我來說過分而不切實際。我會非常感謝任何人在這件事上可能提供的見解!

0

我相信我遇到了類似的事情。我的猜測是,根本原因似乎是一個特點:MVCC。當我混淆模式,或只刪除表然後重新創建時,Visual Studio通常會將其保持爲打開狀態。我只是重新啓動服務,一切都很好。

1

我遇到了同樣的問題,並驗證了Beau's(原始)修補程序。然而,我發現了一個簡單的解決方案:在事務提交後處理命令!然後重新連接不再需要。

mtransaction = Connection.BeginTransaction(IsolationLevel.Serializable); 
FbCommand command = new FbCommand(commandString, Connection, mtransaction); 
command.ExecuteNonQuery(); 
transaction.Commit(); 
command.Dispose(); // Thus! 
transaction.Dispose(); 

問候, 航標

+0

尼斯找到C#的一面!沒有嘗試過! – Beau