tags:

views:

667

answers:

3

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  A: 

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.

Andrew Siemer
The c# part is not an issue, its just how to keep the table alive across multiple c# round trips.
Jason Coyne
This is the reason I don't suggest using a real temp table. I would start your process by creating a table with a specific name for that particular process...then work off of that table...then kill it at the end of the transaction...or when the transaction fails. Do this in a separate DB for performance and disk maintanence reasons.
Andrew Siemer
+1  A: 

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!

Funka
A: 

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

HLGEM
I cannot do the steps in one stored proc, as the data to insert into the tables is not available yet.
Jason Coyne