Thursday, May 31, 2012

Bulk Insert Into SQL Table using BulkCopy from DataTable

In the previous post ADD, Delete and Update in datatable was posted. Now continuing the previous post for Bulk Insertion from DataTable you can write the following code in a button click to insert into SQL datatable as shown below.

This is a method as shown

private void Bulkinsert()
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[
           "MyConnectionString"].ConnectionString))
        {
            cn.Open();
            using (SqlBulkCopy copy = new SqlBulkCopy(cn))
            {
                copy.ColumnMappings.Add(0,0);
                copy.ColumnMappings.Add(1, 1);
                copy.ColumnMappings.Add(2, 2);
                copy.DestinationTableName = "TableName";
                copy.WriteToServer(dt);
            }
        }
    }

No comments:

Post a Comment