Possible Duplicate:
Any way to SQLBulkCopy “insert or update if exists”?
I am using SQLBulkCopy to insert Bulk records
How can I perform on update (rather than an insert) on records that already exist? Is this possible with SQLBulkCopy?
This is my code for SQLBulkCopy
using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
{
bulkCopy.BatchSize = CustomerList.Count;
bulkCopy.DestinationTableName = "dbo.tCustomers";
bulkCopy.ColumnMappings.Clear();
bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
bulkCopy.ColumnMappings.Add("FirstName", "FirstName");
bulkCopy.ColumnMappings.Add("LastName", "LastName");
bulkCopy.ColumnMappings.Add("Address1", "Address1");
bulkCopy.ColumnMappings.Add("Address2", "Address2");
bulkCopy.WriteToServer(CustomerList);
}
Application Details
- ASP.net MVC 3.0 Razor view Engine
- SQL Server 2008
Thanks to @pst
with his suggestions this is how I implemented, if anyone has to implement similar.
Bulk Insert in to permanent Temp Table
Then call a stored Procedure to Merge the temp table with actual table
This is how I used Merge in my Stored Procedure