下面我們先通過一個簡單的例子說明這個類的使用
DateTime startTime;
protected void Button
{
startTime = DateTime
string SrcConString;
string DesConString;
SqlConnection SrcCon = new SqlConnection();
SqlConnection DesCon = new SqlConnection();
SqlCommand SrcCom = new SqlCommand();
SqlDataAdapter SrcAdapter = new SqlDataAdapter();
DataTable dt = new DataTable();
SrcConString =
ConfigurationManager
DesConString =
ConfigurationManager
SrcCon
SrcCom
SrcCom
SrcCom
SrcCom
SrcAdapter
SrcAdapter
SqlBulkCopy DesBulkOp;
DesBulkOp = new SqlBulkCopy(DesConString
SqlBulkCopyOptions
DesBulkOp
DesBulkOp
new SqlRowsCopiedEventHandler(OnRowsCopied);
DesBulkOp
try
{
DesBulkOp
DesBulkOp
}
catch (Exception ex)
{
}
finally
{
SrcCon
DesCon
}
}
private void OnRowsCopied(object sender
{
lblCounter
TimeSpan copyTime = DateTime
lblCounter
}
接著具體分析這幾行代碼
SqlBulkCopy DesBulkOp;
DesBulkOp = new SqlBulkCopy(DesConString
DesBulkOp
指定操作完成的Timeout時間
DesBulkOp
DesBulkOp
try
{
DesBulkOp
DesBulkOp
}
NotifyAfter屬性指定通知通知事件前處理的數據行數
我們還可以自己定義一個Transaction
SqlTransaction Transaction;
Transaction =
SrcCom
SqlBulkCopy DesBulkOp;
DesBulkOp = new SqlBulkCopy(new SqlConnection(DesConString)
SqlBulkCopyOptions
Transaction);
try
{
//
}
catch{}
finally
{
Transaction
}
另外還有一個SqlBulkCopyColumnMapping類
SqlBulkCopyColumnMapping ColMap = new SqlBulkCopyColumnMapping(
DesBulkOp
或者可以直接添加映射
DesBulkOp
性能問題
我使用上面的例子測試
From:http://tw.wingwit.com/Article/program/net/201311/11914.html