Can SqlBulkCopy create a table from Sql selection

Can SqlBulkCopy create a table, sort of like SELECT INTO?

+6
sqlbulkcopy
source share
2 answers

It seems that SqlBulkCopy cannot create tables on its own. The destination table must be predefined. In the case when the recipient received an auto-increment identifier (int), just use 1 in the select ie statement

SELECT 1, [ColumnName], [ColumnName]... FROM TABLENAME 

SQL Server will handle auto-increment on its own.

0
source share

I think the answer above is not entirely clear.

You must create a table with SQL. There is no other way. And if you just need to create a column structure, then it is quite simple, if your source is on the same server, this is enough:

 Select * from source_table into destination_table where 1=2 

If your source is not on the same server (for example, an excel or dbf file or something else), the easiest way is to connect to it using ODBC (or SQL, if possible) and send it:

  Select * from source_table where 1=2 

and then collect the result in a DataTable. Then in the second step, you must create a stored procedure on the target server, which will take this table as an argument, and then insert it into the new table.

More specifically, try this for the SQL procedure: http://www.builderau.com.au/program/sqlserver/soa/Passing-table-valued-parameters-in-SQL-Server-2008/0,339028455,339282577,00 .htm

And create a SqlCommnand object in C # and add to its parameter set SqlParameter, which is SqlDbType.Structured

I did not go into every detail, but I hope this can help.

0
source share

All Articles