how to SqlBulkCopy using different column between excel and dabase table

Last Reply on Mar 17, 2016 07:33 AM By Shashikant

Posted on Mar 16, 2016 04:24 AM

Can anyone tell me how to add data using excel data with 3 columns into database table that has 9 columns.

I'm using this tutorial to bulk my data.

FYI: I'm totally newbie

Thanks & Regards

Posted on Mar 16, 2016 04:25 AM

Hi,

You need to map the Column name link below as Given in article

sqlBulkCopy.ColumnMappings.Add("Id", "PersonId")
sqlBulkCopy.ColumnMappings.Add("Name", "Name")
sqlBulkCopy.ColumnMappings.Add("Salary", "Salary")

I hope this will help you out.


Posted on Mar 16, 2016 06:59 AM

I did that, but when I debug the code it shows error something like the destination table and source table are not match up.

 

FYI: My excel columns are 3 and my sql table columns are 8(so I think it's because of that)


Posted on Mar 17, 2016 04:06 AM Modified on on Mar 17, 2016 04:11 AM

Hi,

Make sure that all other column must be nullable for which you are not passing value or they must have default constraint

I hope this will help you out.

 

 


Posted on Mar 17, 2016 07:22 AM

I found the issue, it seems that I misstype the column name.

 

@Shshikant: thanks for your attantion and help :)


Posted on Mar 17, 2016 07:33 AM
Instead of saying thanks mark the reply or replies(if multiple) that helped as Answer.