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));
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;
dtCustomers.Rows.Add(drCustomer);
for(int i = 0; i < 2; i++)
var drCustomer = dtCustomers.NewRow();
drCustomer["Code"] ="New_Code_" + i;
drCustomer["FirstName"] = "New_FirstName_" + i;
drCustomer["LastName"] = "New_LastName_" + i;
dtCustomers.Rows.Add(drCustomer);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
ShowData(connection, "Before");
using (var bulk = new BulkOperation(connection))
bulk.DestinationTableName = "Customers";
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));
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;
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,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
command.ExecuteNonQuery();