[英]Entity Framework connection not recognizing Temp Table created
我有一個進程,它從Access數據庫中的每日提要中獲取記錄,並將記錄添加到SQL Server。
負載突然失敗,所以我試圖通過SQL服務器Global Temp Table添加,然后從該臨時表中更快地刪除/插入。
我有以下代碼在TSQL中工作:
IF OBJECT_ID ( 'tempdb.dbo.####TempCountry' ) IS NOT NULL DROP TABLE ##TempCountry;
SELECT * INTO ##TempCountry FROM dbo.Country WHERE 1 = 2;
INSERT INTO ##TempCountry (CountryCode, CountryName) VALUES ('AF','AFGHANISTAN')
SELECT * FROM ##TempCountry
我已經使用EntityFramework連接將其轉換為ExecuteStoreCommand
:
using (MyEntities _ent = new MyEntities ()) {
//Create a temp table
try {
_ent.ExecuteStoreCommand("IF OBJECT_ID ( 'tempdb.dbo.##TempCountry' ) IS NOT NULL DROP TABLE ##TempCountry");
_ent.ExecuteStoreCommand("SELECT * INTO ##TempCountry FROM dbo.Country WHERE 1 = 2");
_ent.ExecuteStoreCommand("INSERT INTO ##TempCountry (CountryCode, CountryName, RegionCode) VALUES ('AF','AFGHANISTAN')");
} catch (Exception ex) {
//Generic error
Console.writeline(" Unknown error inserting Country: {0} - {1} ", sCountryCode, ex.Message);
}
} //EF
當我點擊最后的ExecuteStoreCommand
,代碼拋出一個SqlException:“無效的對象名稱'## TempCountry'。”
但是當我執行“select * from tempdb.dbo.sysobjects”時,我可以看到該表
每個ExecuteStoreCommand
將在不同的會話上執行。 第一個命令完成執行后,表將被刪除。
從MSDN - CREATE TABLE (在備注部分,在臨時表中):
當創建表的會話結束並且所有其他任務已停止引用它們時,將自動刪除全局臨時表。
一種解決方案是在一個SQL批處理中執行所有命令,而不是單獨的命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.