2
我在c#中編寫代碼。臨時表如何在交易前創建,然後在交易中使用
我想在事務開始之前創建臨時表,然後在事務中使用它,但是當我嘗試這樣做時,事務內部出現「表不存在」錯誤。做這件事的正確方法是什麼?
SqlConnection sqlConnection = new SqlConnection("connstring");
sqlConnection.Open();
string temp = string.Format("CREATE TABLE dbo.#temp (id INT);");
DbCommand command = database.GetSqlStringCommand(temp);
database.ExecuteNonQuery(command); //here is the problem when I add argument , transaction it works
//fill data in temporary table
//...
// open transaction
SqlTransaction transaction = sqlConnection.BeginTransaction();
//Here I try to read from temp table I have some DbCommand readCommand
database.ExecuteNonQuery(readCommand, transaction);
我聽說,這可能對性能是有害的:如(http://www.sql-server-performance.com/tips/temp_table_tuning_p1.aspx)「如果你必須使用一個臨時表,做如果你這樣做,那麼它會鎖定一些系統表(syscolumns,sysindexes和syscomments),並阻止其他人執行相同的查詢,極大地損害了併發性和性能。實際上,這會將你的應用程序變爲一個單用戶應用程序「。 – Darqer 2010-09-03 10:58:59
@Darqer你可能能夠創建一個內存臨時表:'DECLARE TABLE @Temp(id INT);'不應該有任何這些副作用 – 2010-09-03 14:22:26
我試過了,但然後我必須在一個浴室內做所有事情這是不可能的。 – Darqer 2010-09-03 19:23:09