When I create temporary tables, I get an error message telling me that the temp table already exists. The temporary table is unique to the session, so it seems that my connection is not closing properly, and I think this may have something to do with the return statement that I have in my using statement.
I have the following code:
using (IDbConnection connection = dbConnectionHandler.CreateConnection()) { connection.Open(); CreateATempTable(); PopulateTempTable(); DataSet ds = CallStoredProcThatUsesTempTable(); return ds; }
I use this kind of code in several places to create a temporary table with the same name.
Unfortunately, I get the following error: There is already an object named '#MyTempTable' in the database .
Now I know that the temporary table is unique to the session, so as soon as the session is closed, it should disappear.
There are three things that I believe can cause this ...
- I need to call connection.Close ()
- I need to put a return statement outside my using statement
- I need to drop the temporary table that I created before returning
Does anyone know who he is? or if this is something that I did not think about?
source share