How to Use BulkCopy Without Having to Create a Destination Table

c# datatable sql sqlbulkcopy

Question

All,

What I have is thisBulkCopy operation:

// Convert the FlexGrid to a DataTable.
DataTable currData = (DataTable)c1ErrFlexGrid.DataSource;

// Insert the data into the database.
SqlBulkCopy SqlBulkIns = new SqlBulkCopy(strConnString, SqlBulkCopyOptions.Default);
SqlBulkIns.BatchSize = 5000;
SqlBulkIns.DestinationTableName = String.Format("dbo.{0}", strTableName);
SqlBulkIns.WriteToServer(currData);

where strConnectionString is an established and legitimate connection string. This has previously been successful when the tablestrTableName exists, and the fields have been specified. Now I want to do this.BulkCopy working on aDataTable that is created at runtime; otherwise known as without creating the table structure in advance in SQL. Can this be done? How, if so?

I appreciate your time.

1
3
1/18/2012 4:15:58 PM

Accepted Answer

This SqlTableCreator object may be used to convert your datatable into a SQL table, after which you can do a bulk insert.

8
1/20/2012 8:53:23 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow