【发布时间】:2011-09-19 18:39:14
【问题描述】:
我有一个从 Access 数据库中的每日提要中获取记录并将记录添加到 sql 服务器的过程。
加载偶尔会失败,因此我尝试通过 SQL 服务器全局临时表添加,然后从该临时表中更快地删除/插入。
我有以下在 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”时我可以看到该表
【问题讨论】:
标签: c# sql-server-2008 entity-framework-4 temp-tables