title: Can a return statement prevent a using statement from closing a connection to a database?
body: <p>When I'm creating temp tables I get an error message telling me that the temp table already exists. The temp table is unique to the session so it seems my connection isn't closing properly and I think it may have something to do with a return statement I have in my using statement.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>using (IDbConnection connection = dbConnectionHandler.CreateConnection())
  {
     connection.Open();
     CreateATempTable();
     PopulateTempTable();
     DataSet ds = CallStoredProcThatUsesTempTable();
     return ds;
  }
  </code></pre>
  
  <p>I use this sort of code in several places to create a temp table with the same name.</p>
  
  <p>Unfortunately, I'm getting the following error: <code>There is already an object named '#MyTempTable' in the database</code>.</p>
  
  <p>Now, I know that the temp table is unique to the session and so once the session is closed it should disappear.</p>
  
  <p>There are three things that I believe might cause this... </p>
  
  <ol>
  <li>I need to call connection.Close()</li>
  <li>I need to place the return statement outside my using statement</li>
  <li>I need to drop the temp table I created prior to returning</li>
  </ol>
  
  <p>Does anyone know which one it is? or if its something I haven't thought of?</p>