tulajaram kulkarni
tulajaram kulkarni

Reputation: 21

insert data into table from a dataset

I have a dataset with 18000 + rows in it. Is there a way to insert it in to the SQL server database table with out going through dataset row by row? The dataset will have exactly the same structure as database table. I have more then 4 columns columns that i get. Any code samples or links would be great. Thank you all very much

Upvotes: 0

Views: 3169

Answers (2)

AnandPhadke
AnandPhadke

Reputation: 13506

Use sqlbulkcopy component of C#.net. Please check the details with examples HERE

Upvotes: 1

Habib
Habib

Reputation: 223422

You can use SQLBulkCopy with the method: SqlBulkCopy.WriteToServer Method (DataTable) - MSDN

Copies all rows in the supplied DataTable to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.

Something like:

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
            {
                bulkCopy.DestinationTableName = 
                    "dbo.BulkCopyDemoMatchingColumns";

                try
                {
                    // Write from the source to the destination.
                    // Check if the data set is not null and tables count > 0 etc
                    bulkCopy.WriteToServer(yourDataSet.Tables[0]);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

Upvotes: 3

Related Questions