<div class="blogpost-body" id="cnblogs_post_body">
<p><strong><span style="color:#ff0000;">自曾列就别往下看 别折腾了 </span></strong></p>
<div>
<strong><span style="color:#ff0000;">使用 SqlBulkCopy ,dataTable 必须跟sql里面的表字段完全一样</span></strong>
</div>
<p>下面在sqlserver中演示</p>
<p>mysql 请google MySqlBulkLoader </p>
<p>oracle 请google OracleBulkCopy</p>
<p>表结构</p>
<div class="cnblogs_code">
<pre class="blockcode"> <span style="color:#0000ff;">DROP</span> <span style="color:#0000ff;">TABLE</span> <span style="color:#ff0000;">[</span><span style="color:#ff0000;">dbo</span><span style="color:#ff0000;">]</span>.<span style="color:#ff0000;">[</span><span style="color:#ff0000;">Product</span><span style="color:#ff0000;">]</span>
<span style="color:#0000ff;">GO</span>
<span style="color:#0000ff;">CREATE</span> <span style="color:#0000ff;">TABLE</span> <span style="color:#ff0000;">[</span><span style="color:#ff0000;">dbo</span><span style="color:#ff0000;">]</span>.<span style="color:#ff0000;">[</span><span style="color:#ff0000;">Product</span><span style="color:#ff0000;">]</span><span style="color:#000000;"> (
</span><span style="color:#ff0000;">[</span><span style="color:#ff0000;">Id</span><span style="color:#ff0000;">]</span> <span style="color:#0000ff;">varchar</span>(<span style="color:#800000;font-weight:bold;">36</span>) <span style="color:#808080;">NOT</span> <span style="color:#0000ff;">NULL</span><span style="color:#000000;"> ,
</span><span style="color:#ff0000;">[</span><span style="color:#ff0000;">Name</span><span style="color:#ff0000;">]</span> <span style="color:#0000ff;">varchar</span>(<span style="color:#800000;font-weight:bold;">255</span>) <span style="color:#808080;">NOT</span> <span style="color:#0000ff;">NULL</span><span style="color:#000000;"> ,
</span><span style="color:#ff0000;">[</span><span style="color:#ff0000;">Price</span><span style="color:#ff0000;">]</span> <span style="color:#0000ff;">decimal</span>(<span style="color:#800000;font-weight:bold;">18</span>,<span style="color:#800000;font-weight:bold;">4</span>) <span style="color:#808080;">NOT</span> <span style="color:#0000ff;">NULL</span><span style="color:#000000;">
)
</span><span style="color:#0000ff;">GO</span>
<span style="color:#0000ff;">ALTER</span> <span style="color:#0000ff;">TABLE</span> <span style="color:#ff0000;">[</span><span style="color:#ff0000;">dbo</span><span style="color:#ff0000;">]</span>.<span style="color:#ff0000;">[</span><span style="color:#ff0000;">Product</span><span style="color:#ff0000;">]</span> <span style="color:#0000ff;">ADD</span> <span style="color:#0000ff;">PRIMARY</span> <span style="color:#0000ff;">KEY</span> (<span style="color:#ff0000;">[</span><span style="color:#ff0000;">Id</span><span style="color:#ff0000;">]</span><span style="color:#000000;">)
</span><span style="color:#0000ff;">GO</span></pre>
</div>
<p>批量添加</p>
<div class="cnblogs_code">
<pre class="blockcode"> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Insert<T>(<span style="color:#0000ff;">string</span> connectionString, List<T> dataList, <span style="color:#0000ff;">string</span> destinationTableName, <span style="color:#0000ff;">int</span> batchSize = <span style="color:#800080;">0</span><span style="color:#000000;">)
{
DataTable dataTable </span>=<span style="color:#000000;"> ConvertToDataTable(dataList);
Insert(connectionString, dataTable, destinationTableName, batchSize);
}
</span><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Insert(<span style="color:#0000ff;">string</span> connectionString, DataTable dataTable, <span style="color:#0000ff;">string</span> destinationTableName, <span style="color:#0000ff;">int</span> batchSize = <span style="color:#800080;">0</span><span style="color:#000000;">)
{
</span><span style="color:#0000ff;">using</span> (SqlConnection connection = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SqlConnection(connectionString))
{
</span><span style="color:#0000ff;">if</span> (connection.State !=<span style="color:#000000;"> ConnectionState.Open)
{
connection.Open();
}
</span><span style="color:#0000ff;">using</span> (SqlTransaction transaction =<span style="color:#000000;"> connection.BeginTransaction())
{
</span><span style="color:#0000ff;">using</span> (SqlBulkCopy bulkCopy = <span style="color:#0000ff;">new</span><span style="color:#000000;"> SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction))
{
bulkCopy.BatchSize </span>=<span style="color:#000000;"> batchSize;
bulkCopy.DestinationTableName </span>=<span style="color:#000000;"> destinationTableName;
</span><span style="color:#0000ff;">try</span><span style="color:#000000;">
{
bulkCopy.WriteToServer(dataTable);
transaction.Commit();
}
|
|