SqlBulkCopy结合事务批量保存数据到数据库 ///<summary>
///保存到数据库
///</summary>
///<param name="mDCCampaignList"></param>
skd///<param name="mDCAdgroupList"></param>
///<param name="mDCKeywordList"></param>
private void SaveToServer(IList<DCCampaign> mDCCampaignList, IList<DCADGroup> mDCAdgroupList, IList<DCKeyword> mDCKeywordList) {
using (SqlConnection conn = new DBHelper().GetSqlConnection())
labour{
conn.Open();
anacondausing (SqlTransaction tran = conn.BeginTransaction())
{
try
{
testdaf报名using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran))
{
//設置⼀个批,写⼊多少条记录
bulkCopy.BatchSize = 10000;
bulkCopy.BulkCopyTimeout = 60;南美栗鼠
////設置 NotifyAfter 属性,以便在每拷贝 10000 条记录⾄数据表後,呼叫事件处理函数
//bulkCopy.NotifyAfter = 10000;
//bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
/
/設置要写⼊的数据库
在线翻译网bulkCopy.DestinationTableName = "dbo.DC_Campaign";
DataTable dt = mDCCampaignList.ToDataTable();
bulkCopy.WriteToServer(dt);
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran))
{
扯淡的英文//設置⼀个批,写⼊多少条记录
bulkCopy.BatchSize = 10000;
bulkCopy.BulkCopyTimeout = 60;
////設置 NotifyAfter 属性,以便在每拷贝 10000 条记录⾄数据表後,呼叫事件处理函数
/abara
/bulkCopy.NotifyAfter = 10000;
//bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
//設置要写⼊的数据库
bulkCopy.DestinationTableName = "dbo.DC_ADGroup";
DataTable dt = mDCAdgroupList.ToDataTable();
bulkCopy.WriteToServer(dt);
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran))
{
//設置⼀个批,写⼊多少条记录
bulkCopy.BatchSize = 100000;
bulkCopy.BulkCopyTimeout = 60;
////設置 NotifyAfter 属性,以便在每拷贝 10000 条记录⾄数据表後,呼叫事件处理函数
//bulkCopy.NotifyAfter = 10000;
openfeint//bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
//設置要写⼊的数据库
bulkCopy.DestinationTableName = "dbo.DC_Keyword";
DataTable dt = mDCKeywordList.ToDataTable();
polly
bulkCopy.WriteToServer(dt);
}
//事务提交
tran.Commit();
}
catch(Exception e)
{
//事务回滚
tran.Rollback();
throw e;
}
finally
{
tran.Dispo();
}
}
}
}