---
title: "Bulkcopy with primary key not working"  
description: "Bulkcopy with primary key not working"  
author: "Takeshi Okada"  
published: 2015-05-25  
updated: 2015-05-25  
canonical: https://www.mindstick.com/forum/23261/bulkcopy-with-primary-key-not-working  
category: "mssql server"  
tags: ["sql server 2008"]  
reading_time: 1 minute  

---

# Bulkcopy with primary key not working

I want to do the bulkcopy, from a [datatable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) in my c#.

I have a [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table), with columns and a [primary key](https://www.mindstick.com/blog/214/primary-key).

When I have primary key in my table, I got [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) 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.Clear();            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](https://www.mindstick.com/blog/63570/5-ways-to-make-money-with-the-extra-space-in-your-home) ID primary key,

***Note:*** when I [delete](https://www.mindstick.com/forum/33620/how-to-delete-record-from-list-in-mvc-using-ajax) my primary key in the database, everything works perfectly

## Replies

### Reply by Anonymous User

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");
```


---

Original Source: https://www.mindstick.com/forum/23261/bulkcopy-with-primary-key-not-working

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
