As shown in the answer, ExecuteNonQuery uses sp_executesql when CommandType is CommandType.Text and the command has parameters.
The C # code in this question does not explicitly CommandType and Text by default , so most likely the end result of the code is that CREATE TABLE #ConnectionContextInfo wrapped in sp_executesql . You can check it in SQL Profiler.
It is well known that sp_executesql works in its own field (in fact, it is a nested stored procedure). Find "sp_executesql temporary table". Here is one example: Run sp_executeSql to select ... in #table, but cannot select the tempo table data
So, the temporary table #ConnectionContextInfo is created in the nested sp_executesql and is automatically deleted as soon as sp_executesql returned. The following query executed by adapter.Fill does not see this temp table.
What to do?
Ensure that the CREATE TABLE #ConnectionContextInfo not enclosed in sp_executesql .
In your case, you can try to split one batch containing both CREATE TABLE #ConnectionContextInfo and INSERT INTO #ConnectionContextInfo into two batches. The first packet / query will contain only the CREATE TABLE statement without any parameters. The second packet / request would contain an INSERT INTO with parameters (s).
I'm not sure if this will help, but worth a try.
If this does not work, you can create one T-SQL batch that creates a temporary table, inserts data into it, and calls your stored procedure. All in one SqlCommand, all in one batch. All of this SQL will be wrapped in sp_executesql , but it does not matter, because the area in which the temporary table is created will be the same area in which the stored procedure is called. Technically, this will work, but I would not recommend following this path.
There is no answer to the question, but a proposal to solve the problem.
Honestly, the whole approach looks strange. If you want to pass some data to a stored procedure, why not use the parameters of this stored procedure. This is what they are intended for - to transfer data to the procedure. There is no real need to use a temporary table for this. You can use the table-valued parameter ( T-SQL ,. NET ) if the data you pass is complex. This is definitely overkill if it's just a Username .
In your stored procedure, you need to know the temporary table, it must know its name and structure, so I donβt understand what the problem is with the explicit table parameter. Even your procedure code has not changed much. You would use @ConnectionContextInfo instead of #ConnectionContextInfo .
Using temporary tables for what you described makes sense to me only if you are using SQL Server 2005 or earlier, which does not have table parameters. They were added in SQL Server 2008.