using System.Collections.Generic;
using System.Data.DataSetExtensions;
using System.Data.SqlClient;
public static void Main()
DataTable dtCustomers = new DataTable("Customers");
dtCustomers.Columns.Add("CustomerID", typeof(int));
dtCustomers.Columns.Add("Code", typeof(string));
dtCustomers.Columns.Add("FirstName", typeof(string));
dtCustomers.Columns.Add("LastName", typeof(string));
dtCustomers.Columns.Add("CreatedDate", typeof(DateTime));
dtCustomers.Columns.Add("UpdatedDate", typeof(DateTime));
for(int i = 0; i < 2; i++)
var drCustomer = dtCustomers.NewRow();
drCustomer["CustomerID"] = (i + 1);
drCustomer["Code"] = "Code_" + i;
drCustomer["FirstName"] = "Updated_FirstName_" + i;
drCustomer["LastName"] = "Updated_LastName_" + i;
drCustomer["CreatedDate"] = DateTime.Now;
drCustomer["UpdatedDate"] = DateTime.Now;
dtCustomers.Rows.Add(drCustomer);
for(int i = 0; i < 2; i++)
var drCustomer = dtCustomers.NewRow();
drCustomer["CustomerID"] = (i + 1);
drCustomer["Code"] ="New_Code_" + i;
drCustomer["FirstName"] = "New_FirstName_" + i;
drCustomer["LastName"] = "New_LastName_" + i;
drCustomer["CreatedDate"] = DateTime.Now;
drCustomer["UpdatedDate"] = DateTime.Now;
dtCustomers.Rows.Add(drCustomer);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
ShowData(connection, "Before");
using (var bulk = new BulkOperation(connection))
bulk.DestinationTableName = "Customers";
var columnMapping1 = new ColumnMapping("UpdatedDate");
var columnMapping2 = new ColumnMapping("CreatedDate");
columnMapping1.IgnoreOnSynchronizeInsert = true;
columnMapping2.IgnoreOnSynchronizeUpdate = true;
bulk.ColumnMappings.Add(columnMapping1);
bulk.ColumnMappings.Add(columnMapping2);
bulk.ColumnMappings.Add("Code", true);
bulk.ColumnMappings.Add("CustomerID");
bulk.ColumnMappings.Add("FirstName");
bulk.ColumnMappings.Add("LastName");
bulk.BulkSynchronize(dtCustomers);
ShowData(connection, "After");
public static void ShowData(SqlConnection connection, string text)
using (var command = connection.CreateCommand())
var sqlDataAdapter = new SqlDataAdapter(command);
ds.Tables[0].TableName = "1 - Customers " + text;
FiddleHelper.WriteTable(ds.Tables[0]);
public static void GenerateCustomers(int count, bool useOld = false)
DataTable dtTable = new DataTable("Customers");
dtTable.Columns.Add("CustomerID", typeof(int));
dtTable.Columns.Add("Code", typeof(string));
dtTable.Columns.Add("FirstName", typeof(string));
dtTable.Columns.Add("LastName", typeof(string));
dtTable.Columns.Add("CreatedDate", typeof(DateTime));
dtTable.Columns.Add("UpdatedDate", typeof(DateTime));
for(int i = 0; i < count; i++)
var drCustomer = dtTable.NewRow();
drCustomer["CustomerID"] = (i + 1);
drCustomer["Code"] = "Code_" + i;
drCustomer["FirstName"] = "FirstName_" + i;
drCustomer["LastName"] = "LastName_" + i;
drCustomer["CreatedDate"] = DateTime.Now.AddDays(-2);
dtTable.Rows.Add(drCustomer);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
using (var bulk = new BulkOperation(connection))
bulk.DestinationTableName = "Customers";
bulk.BulkInsert(dtTable);
public static void GenerateDatabase()
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
using (var command = connection.CreateCommand())
[CustomerID] [INT] IDENTITY(1,1) NOT NULL,
[Code] [NVARCHAR](MAX) NULL,
[FirstName] [NVARCHAR](MAX) NULL,
[LastName] [NVARCHAR](MAX) NULL,
[CreatedDate] DateTime NULL,
[UpdatedDate] DateTime NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
command.ExecuteNonQuery();