问题描述
我正在尝试使用bulkcopy将数据从datatable存储到数据库。当数据表中的所有数据格式正确时,它工作正常。如果任何记录不正确,例如PersonName包含任何Number值,那么我想从datatable中删除该记录并继续使用bulkcopy。请帮帮我..
我的代码就是这个....
'预郎= CS> 私有跨度> 空隙跨度> btnSQLBulkCopyInsert_Click(对象发件人,EventArgs e)
{
// 获取DataTable
DataTable dtInsertRows = GetDataTable();
使用跨度>(SqlBulkCopy的SBC = 新跨度> SqlBulkCopy的(的connectionString))
{
sbc.DestinationTableName = 人;
// 一次性处理的记录数
sbc.BatchSize = 2 ;
// 将源列从DataTabel映射到SQL Server 2005人员表中的目标列
sbc.ColumnMappings.Add( PersonId, PersonId);
sbc.ColumnMappings.Add( PersonName, PersonName);
// 必须通知客户有关其状态的记录数
sbc.NotifyAfter = dtInsertRows.Rows.Count;
// NotifyAfter处理完记录数后被触发的事件。
sbc.SqlRowsCopied + = new SqlRowsCopiedEventHandler(sbc_SqlRowsCopied);
// 最后写入服务器
sbc.WriteToServer( dtInsertRows);
sbc.Close();
}
}
void sbc_SqlRowsCopied( object 发件人,SqlRowsCopiedEventArgs E)
{
MessageBox.Show( 跨度> 受影响的记录数: + e.RowsCopied.ToString());
}
I am trying to store data from datatable to database using bulkcopy. It is working fine when all data in datatable are in correct format. If any records is incorrect such as PersonName contain any Number value that time I want to remove that records from datatable and continue with bulkcopy. Please help me..
My code is this....
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()); }
这篇关于批量复制时删除错误的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!