// prepare your table
var recipientTable = new DataTable();
recipientTable.Columns.Add("Id", typeof(int));
recipientTable.Columns.Add("MId", typeof(int));
recipientTable.Columns.Add("CGuid", typeof(Guid));
recipientTable.Columns.Add("DId", typeof(int));
recipientTable.Columns.Add("PId", typeof(int));
recipientTable.Columns.Add("CId", typeof(int));
recipientTable.Columns.Add("Status", typeof(int));
recipientTable.Columns.Add("Name", typeof(string));
recipientTable.Columns.Add("UToken", typeof(Guid));
recipientTable.Columns.Add("PDate", typeof(DateTime));
recipientTable.Columns.Add("MProperties", typeof(string));
// Step1 TODO :count total record,
// step2 devide it by 2000,
// step3 loop through it by 2000 chunk add it recipientTable, and execute query
int commitBatchSize = 2000;
IEnumerable<Employees> recipientTableTypes = employees as Employees[] ?? employees.ToArray();
employees.ForEach(r => recipientTable.Rows.Add(null, r.Id, null, r.Status, r.DId, null,
r.CId, r.Name, null, DateTime.UtcNow, r.MailingProperties));
int numberOfPages = (recipientTable.Rows.Count / commitBatchSize) + (recipientTable.Rows.Count % commitBatchSize == 0 ? 0 : 1);
for (int pageIndex = 0; pageIndex < numberOfPages; pageIndex++)
{
DataTable dt = recipientTable.AsEnumerable().Skip(pageIndex * commitBatchSize).Take(commitBatchSize).CopyToDataTable();
BulkInsert(dt, connectionString);
}
_______________________________________________________
Method:
public void BulkInsert(DataTable dt, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// make sure to enable triggers
SqlBulkCopy bulkCopy = new SqlBulkCopy(connection,
SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.UseInternalTransaction,
null
);
// set the destination table name
bulkCopy.DestinationTableName = "MailingListRecipientHistory";
connection.Open();
// write the data in the "dataTable"
bulkCopy.WriteToServer(dt);
connection.Close();
}
// reset
//dt.Clear();
}
var recipientTable = new DataTable();
recipientTable.Columns.Add("Id", typeof(int));
recipientTable.Columns.Add("MId", typeof(int));
recipientTable.Columns.Add("CGuid", typeof(Guid));
recipientTable.Columns.Add("DId", typeof(int));
recipientTable.Columns.Add("PId", typeof(int));
recipientTable.Columns.Add("CId", typeof(int));
recipientTable.Columns.Add("Status", typeof(int));
recipientTable.Columns.Add("Name", typeof(string));
recipientTable.Columns.Add("UToken", typeof(Guid));
recipientTable.Columns.Add("PDate", typeof(DateTime));
recipientTable.Columns.Add("MProperties", typeof(string));
// Step1 TODO :count total record,
// step2 devide it by 2000,
// step3 loop through it by 2000 chunk add it recipientTable, and execute query
int commitBatchSize = 2000;
IEnumerable<Employees> recipientTableTypes = employees as Employees[] ?? employees.ToArray();
employees.ForEach(r => recipientTable.Rows.Add(null, r.Id, null, r.Status, r.DId, null,
r.CId, r.Name, null, DateTime.UtcNow, r.MailingProperties));
int numberOfPages = (recipientTable.Rows.Count / commitBatchSize) + (recipientTable.Rows.Count % commitBatchSize == 0 ? 0 : 1);
for (int pageIndex = 0; pageIndex < numberOfPages; pageIndex++)
{
DataTable dt = recipientTable.AsEnumerable().Skip(pageIndex * commitBatchSize).Take(commitBatchSize).CopyToDataTable();
BulkInsert(dt, connectionString);
}
_______________________________________________________
Method:
public void BulkInsert(DataTable dt, string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// make sure to enable triggers
SqlBulkCopy bulkCopy = new SqlBulkCopy(connection,
SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.UseInternalTransaction,
null
);
// set the destination table name
bulkCopy.DestinationTableName = "MailingListRecipientHistory";
connection.Open();
// write the data in the "dataTable"
bulkCopy.WriteToServer(dt);
connection.Close();
}
// reset
//dt.Clear();
}
No comments:
Post a Comment