private void btnSQLBulkCopyInsert_Click(object sender, EventArgs e)
{
// Get the DataTable
DataTable dtInsertRows = GetDataTable();
using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString))
{
sbc.DestinationTableName = "Person";
// Number of records to be processed in one go
sbc.BatchSize = 2;
// Map the Source Column from DataTabel to the
Destination Columns in SQL Server 2005 Person Table
sbc.ColumnMappings.Add("PersonId", "PersonId");
sbc.ColumnMappings.Add("PersonName", "PersonName");
// Number of records after which client has to be
notified about its status
sbc.NotifyAfter = dtInsertRows.Rows.Count;
// Event that gets fired when NotifyAfter number of
records are processed.
sbc.SqlRowsCopied+=new
SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);
// Finally write to server
sbc.WriteToServer(dtInsertRows);
sbc.Close();
}
}
void sbc_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
MessageBox.Show("Number of records affected : " +
e.RowsCopied.ToString());
}
How to use SQLBulkCopy of ADO.NET 2.0
Subscribe to:
Post Comments (Atom)
1 comments:
Thanks for nice post. i need same requirement in my current project..
Thanks again
Mark
Post a Comment