工作中要频繁的处理一些数据导入,又不想手工去做,因此用了神器SqlBulkCopy。在MSDN查看了此类的帮助文档几经波折终于搞定,记录下来方便以后查阅。

MSDN实例:

 using System.Data.SqlClient;

 class Program
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a sourceConnection to the AdventureWorks database.
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open(); // Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
sourceConnection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart); // Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand(
"SELECT ProductID, Name, " +
"ProductNumber " +
"FROM Production.Product;", sourceConnection);
SqlDataReader reader =
commandSourceData.ExecuteReader(); // Open the destination connection. In the real world you would
// not use SqlBulkCopy to move data from one table to the other
// in the same database. This is for demonstration purposes only.
using (SqlConnection destinationConnection =
new SqlConnection(connectionString))
{
destinationConnection.Open(); // Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns"; try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Close the SqlDataReader. The SqlBulkCopy
// object is automatically closed at the end
// of the using block.
reader.Close();
}
} // Perform a final count on the destination
// table to see how many rows were added.
long countEnd = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Ending row count = {0}", countEnd);
Console.WriteLine("{0} rows were added.", countEnd - countStart);
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
}
} private static string GetConnectionString()
// To avoid storing the sourceConnection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}

MSDN 地址:https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlbulkcopy(v=vs.110).aspx

自己的测试代码:

 /// <summary>
/// 将DataTable表数据插入数据库目标表
/// DataTable Name 和数据库目标表名称一致,DataTable列名为目标数据库目标表字段
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="dbFlag">数据库标识</param>
/// <returns></returns>
public static bool MassInsert(DataTable dt, string dbFlag)
{
if (dt == null || dt.Rows.Count == )
{
Logger.Write("DataTable为空或无填充行", "大数据操作", "");
return false;
}
if (String.IsNullOrEmpty(dt.TableName))
{
Logger.Write("DataTableName为空无法确定目标数据库表", "大数据操作", "");
return false;
}
SqlConnection con = HelpGetSqlCon(dbFlag);//动态获取SqlConnection对象
DateTime time = DateTime.Now;
SqlTransaction tra = con.BeginTransaction();//事务
SqlBulkCopy sqlBC = new SqlBulkCopy(con, SqlBulkCopyOptions.Default, tra);
try
{
sqlBC.DestinationTableName = dt.TableName;
//一次批量的插入的数据量
sqlBC.BatchSize = dt.Rows.Count;
sqlBC.WriteToServer(dt);
Logger.Write("【MassInsert】[" + dt.Rows.Count + "]条数据共耗时[" + (DateTime.Now - time).TotalSeconds.ToString() + "]秒", "", ""); //记录日志
tra.Commit();
return true;
}
catch (Exception ex)
{
tra.Rollback();
Logger.WriteException("MassInsert异常", "", ex, "");
return false;
}
finally
{
dt.Clear();
dt.Dispose();
sqlBC.Close();
con.Dispose();
con.Close();
}
}

以上代码可以插入主键,如果主键重复则事务回滚

正常测试结果:

【2015-04-29 12:49:44】
【MassInsert】[182689]条数据共耗时[3.4911996]秒

主键重复测试结果:

【2015-04-29 12:41:16】

Message:MassInsert异常

Ex.Message:违反了 PRIMARY KEY 约束“PK_AUTHCODES”。不能在对象“dbo.AuthCodes”中插入重复键。重复键值为 (1)。
语句已终止。

Ex.StackTrace: 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
在 System.Data.SqlClient.SqlBulkCopy.RunParser(BulkCopySimpleResultSet bulkCopyHandler)
在 System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinuedOnSuccess(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
在 System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
在 System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
在 System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
在 System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
在 System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
在 System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
在 System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)
在 System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table)

最新文章

  1. Java企业实训 - 01 - Java前奏
  2. D3树状图异步按需加载数据
  3. 理解 Linux 网络栈(2):非虚拟化Linux 环境中的 Segmentation Offloading 技术
  4. emoji表情引发的JNI崩溃
  5. 英語版Windows Server 2012 R2を日本語化する手順
  6. CSS控制文本超出指定宽度显示省略号和文本不换行
  7. CCF 201403-2 窗口 (STL模拟)
  8. lucene全文搜索之四:创建索引搜索器、6种文档搜索器实现以及搜索结果分析(结合IKAnalyzer分词器的搜索器)基于lucene5.5.3
  9. java 字符串全排列 和 去重
  10. 搭建es6开发与非开发环境babel-browser
  11. Data Base mongodb driver2.5环境注意事项
  12. Confluence 6 管理 Atlassian 提供的 App
  13. Spark实战1
  14. 【WIN10】移植opencc到WIN10-UWP,實現自己的繁簡轉換工具
  15. Android 线程 thread 两种实现方法
  16. Asp.Net Mvc表单提交(批量提交)
  17. c# 异步编程demo (async await)
  18. android ReactNative之Cannot find entry file index.android.js in any of the roots
  19. 百度地图api描绘车辆历史轨迹图
  20. Django 自定义分页类

热门文章

  1. Json日期格式 转化为 YYYY-MM-DD-hh-mm-ss
  2. Openstack之Nova创建虚机流程分析
  3. 第九节课-CNN架构
  4. 微信小程序获取用户信息,解密encryptedData 包括敏感数据在内的完整用户信息的加密数据
  5. ubuntu 14.04 建立wifi热点
  6. handle 机制的原理是什么
  7. spring cloud 基本小结
  8. scala学习手记4 - Java基本类型对应的scala类
  9. 图片加载之Picasso使用
  10. 使用SpringMVC报错 Error creating bean with name &#39;conversionService&#39; defined in class path resource [springmvc.xml]