Copy Data from DataSet to SQL Table

Some time we need to insert bunch of data directly from DataTable or DataSet to SQL Table, below is the code for Inserting data at once using Bulk copy Method:

// Bulk Copy to SQL Server
                    using (SqlBulkCopy bulkCopy =
                               new SqlBulkCopy(con))
                    {
                        bulkCopy.DestinationTableName = tableName;
                        foreach (DataColumn col in dt.Columns)
                        {
                            bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
                        }
                        bulkCopy.WriteToServer(dt);
                    }

Comments

  1. Yes it works fine, thank for the Code......

    ReplyDelete
  2. You are welcome Amit. Keep visiting for more technical stuffs. I appreciate your valuable comments...

    ReplyDelete

Post a Comment

Thanking you for the comment.

Technology