I've got a process that takes records from a daily feed in an Access database and adds the records to a sql server.
The load occationally fails, so I'm trying to add via a SQL server Global Temp Table and then a much quicker Delete/Insert from that temp table.
I've got the following code that works in 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
And I've translated this to ExecuteStoreCommand's using an EntityFramework connection:
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
When I hit the final ExecuteStoreCommand, the code throws a SqlException: "Invalid object name '##TempCountry'."
But I can see the table when I execute a "Select * from tempdb.dbo.sysobjects"