static void BulkInsert()
{
string csvConnectionString = string.Format(@"Uri='sampleCSV.csv';Trim Values=true");
using (C1CSVConnection con = new C1CSVConnection(csvConnectionString))
{
// Populate Datatable
C1CSVDataAdapter adapter = new C1CSVDataAdapter(con, "SELECT * FROM sampleCSV");
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
// Create Insert command
adapter.InsertCommand = new C1CSVCommand(con);
adapter.InsertCommand.CommandText = "INSERT INTO sampleCSV
(id,year,industry_code,industry_name,rme_size_grp,variable,value,unit) VALUES
(@id,@year, @industry_code, @industry_name, @rme_size_grp, @variable, @value, @unit)";
// Add query parameters
adapter.InsertCommand.Parameters.Add("@id", "id");
adapter.InsertCommand.Parameters.Add("@year", "year");
adapter.InsertCommand.Parameters.Add("@industry_code", "industry_code");
adapter.InsertCommand.Parameters.Add("@industry_name", "industry_name");
adapter.InsertCommand.Parameters.Add("@rme_size_grp", "rme_size_grp");
adapter.InsertCommand.Parameters.Add("@variable", "variable");
adapter.InsertCommand.Parameters.Add("@value", "value");
adapter.InsertCommand.Parameters.Add("@unit", "unit");
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
// Insert one row
DataRow sampleCSVRow1 = dataTable.NewRow();
sampleCSVRow1["id"] = 101;
sampleCSVRow1["year"] = 2021;
sampleCSVRow1["industry_code"] = "A";
sampleCSVRow1["industry_name"] = "Agriculture, Forestry and Fishing";
sampleCSVRow1["rme_size_grp"] = "c_6-9";
sampleCSVRow1["variable"] = "Total assets";
sampleCSVRow1["value"] = "45";
sampleCSVRow1["unit"] = "DOLLARS(millions)";
dataTable.Rows.Add(sampleCSVRow1);
// Insert another row
DataRow sampleCSVRow2 = dataTable.NewRow();
sampleCSVRow2["id"] = 102;
sampleCSVRow2["year"] = 2021;
sampleCSVRow2["industry_code"] = "B";
sampleCSVRow2["industry_name"] = "Mining";
sampleCSVRow2["rme_size_grp"] = "a_0";
sampleCSVRow2["variable"] = "Total income";
sampleCSVRow2["value"] = "10";
sampleCSVRow2["unit"] = "DOLLARS(millions)";
dataTable.Rows.Add(sampleCSVRow2);
// Set batch size and update database
adapter.UpdateBatchSize = 2;
adapter.Update(dataTable);
Console.WriteLine("Bulk insert successful! \n\n");
}
}