How do I use a temp table across multiple c# calls

允我心安 提交于 2019-12-08 08:19:18

问题


I have a C# application, using ADO.Net to connect to MSSQL

I need to create the table (with a dynamic number of columns), then insert many records, then do a select back out of the table.

Each step must be a separate C# call, although I can keep a connection/transaction open for the duration.


回答1:


There are two types of temp tables in SQL Server, local temp tables and global temp tables. From the BOL:

Prefix local temporary table names with single number sign (#tablename), and prefix global temporary table names with a double number sign (##tablename).

Local temp tables will live for just your current connection. Globals will be available for all connections. Thus, if you re-use (and you did say you could) the same connection across your related calls, you can just use a local temp table without worries of simultaneous processes interfering with each others' temp tables.

You can get more info on this from the BOL article, specifically under the "Temporary Tables" section about halfway down.

Best of luck!
-f!




回答2:


The issue is that #Temp tables exist only within the Connection AND the Scope of the execution. When the first call from C# to SQL completes, control passes up to a higher level of scope.

This is just as if you had a T-SQL script that called two stored procedures. Each SP created a table named #MyTable. The second SP is referencing a completly different table than the first SP.
However, if the parent T-SQL code created the table, both SP's could see it, but they can't see each others.

The solution here is to use ##Temp tables. They cross scope and connections. The danger though is that if you use a hard coded name, then two instances of your program running at the same time could see the same table. So dynamically set the table name to something that will be always be unique.




回答3:


You might take a look at the repository pattern as far as dealing with this concept in C#. This allows you to have a low level repository layer for data access where each method performs a task. But the connection is passed in to the method and actual actions are performed with in a transaction scope. This means you can theoretically call many different methods in your data access layer (implemented as repository) and if any of them fail you can roll back the whole operation.

http://martinfowler.com/eaaCatalog/repository.html

The other aspects of your question would be handled by standard sql where you can dynamically create a table, insert into it, delete from it, etc. The tricky part here is keeping one transaction away from another transaction. You might look to using temp tables...or you might simply have a 2nd database specifically for performing this dynamic table concept.




回答4:


Personaly I think you are doing this the hard way. Do all the steps in one stored proc.



来源:https://stackoverflow.com/questions/1268522/how-do-i-use-a-temp-table-across-multiple-c-sharp-calls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!