Home > DeveloperSection > Forums > Bulkcopy with primary key not working
Takeshi Okada
Takeshi Okada

Total Post:89

Posted on    May-25-2015 7:39 AM

 MSSQL Server SQL Server 2008 

 1 Reply(s)
 704  View(s)
Rate this:

I want to do the bulkcopy, from a datatable in my c#.

I have a database table, with columns and a primary key.

When I have primary key in my table, I got exception because the table has 6 columns, while my datatable has just 5.

what should I do please?

private DataTable getBasicDataTable()


            DataTable dataTable = new DataTable();


            dataTable.Columns.Add("customerID", typeof(int));

            dataTable.Columns.Add("firstName", typeof(string));

            dataTable.Columns.Add("lastName", typeof(string));

            dataTable.Columns.Add("showsNumber", typeof(int));

            dataTable.Columns.Add("visitNumber", typeof(int));

            dataTable.Columns.Add("cancellation", typeof(int));

            return dataTable;


but in my database table, I have the exact same columns, but with extra ID primary key,

Note:  when I delete my primary key in the database, everything works perfectly

ezra heywood
ezra heywood

Total Post:145

Posted on    May-25-2015 8:23 AM

I found the solution myself

bc.ColumnMappings.Add("customerID", "customerID");

                sbc.ColumnMappings.Add("firstName", "firstName");

                sbc.ColumnMappings.Add("lastName", "lastName");

                sbc.ColumnMappings.Add("showsNumber", "showsNumber");

                sbc.ColumnMappings.Add("visitNumber", "visitNumber");

                sbc.ColumnMappings.Add("cancellation", "cancellation");

Don't want to miss updates? Please click the below button!

Follow MindStick