1. 程式人生 > >關於批量匯入資料類SqlBulkCopy 匯入資料時增加額外的列

關於批量匯入資料類SqlBulkCopy 匯入資料時增加額外的列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data;

namespace 樣本採購訂單
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch st = new Stopwatch();
            st.Start();

            
string connectionString = @"Data Source=192.168.0.20;Initial Catalog=misdb;User ID=sa;Password=123456;Replication=True"; string SqlConnectionStringTarget = "Data Source=192.168.0.20;Initial Catalog=Basedb;User ID=sa;Password=123456;Replication=True"; //DataTable sourceData = new DataTable();
// using (SqlConnection sourceConnection = new SqlConnection(connectionString)) { //源表查詢資料 SqlCommand myCommand = new SqlCommand(" select * from ReceDetail ", sourceConnection); sourceConnection.Open(); SqlDataReader reader
= myCommand.ExecuteReader(CommandBehavior.CloseConnection); //sourceData.Load(reader); //sourceConnection.Close(); // 目的 /* * 這裡用的是同一個連線, * * 如果不是同一個資料庫,則用不同的連線 * * 注意:一定要按照我的連線字串的格式來寫,不然是不行的 * * */ using (SqlConnection Target = new SqlConnection(SqlConnectionStringTarget)) { // 開啟連線 //Target.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(Target.ConnectionString, SqlBulkCopyOptions.UseInternalTransaction)) { //源列欄位:A,目標資料來源欄位:B SqlBulkCopyColumnMapping m1 = new SqlBulkCopyColumnMapping("ShopCode", "ShopName"); SqlBulkCopyColumnMapping m2 = new SqlBulkCopyColumnMapping("ReceNo", "ReceNo"); SqlBulkCopyColumnMapping m4 = new SqlBulkCopyColumnMapping("TranDate", "TranDate"); SqlBulkCopyColumnMapping m6 = new SqlBulkCopyColumnMapping("RealAmount", "RealAmount"); SqlBulkCopyColumnMapping m7 = new SqlBulkCopyColumnMapping("DiscountType", "DiscountType"); SqlBulkCopyColumnMapping m8 = new SqlBulkCopyColumnMapping("Discount", "Discount"); SqlBulkCopyColumnMapping m9 = new SqlBulkCopyColumnMapping("ReceType", "ReceType"); SqlBulkCopyColumnMapping m10 = new SqlBulkCopyColumnMapping("AccountDiff", "AccountName"); SqlBulkCopyColumnMapping m12 = new SqlBulkCopyColumnMapping("CheckNo", "CheckNo"); SqlBulkCopyColumnMapping m13 = new SqlBulkCopyColumnMapping("Comment", "Comment"); SqlBulkCopyColumnMapping m16 = new SqlBulkCopyColumnMapping("Operator", "CreatePerson"); SqlBulkCopyColumnMapping m17 = new SqlBulkCopyColumnMapping("EnableFlag", "DeleteFlag"); SqlBulkCopyColumnMapping m18 = new SqlBulkCopyColumnMapping("CreateDate", "CreateTime"); bulkCopy.ColumnMappings.Add(m1); bulkCopy.ColumnMappings.Add(m2); bulkCopy.ColumnMappings.Add(m4); bulkCopy.ColumnMappings.Add(m6); bulkCopy.ColumnMappings.Add(m7); bulkCopy.ColumnMappings.Add(m8); bulkCopy.ColumnMappings.Add(m9); bulkCopy.ColumnMappings.Add(m10); bulkCopy.ColumnMappings.Add(m12); bulkCopy.ColumnMappings.Add(m13); bulkCopy.ColumnMappings.Add(m16); bulkCopy.ColumnMappings.Add(m17); bulkCopy.ColumnMappings.Add(m18); //有多列時類推: /** * SqlBulkCopyColumnMapping m1 = new SqlBulkCopyColumnMapping("A","B"); * bulkCopy.ColumnMappings.Add(m1); * * */ //目標資料表 bulkCopy.DestinationTableName = "Fina_ReceDetail"; //每次更新五條 bulkCopy.BatchSize = 5; //每更新十條的時候就發出通知 bulkCopy.NotifyAfter = 10; //下面的函式隨時都在監督著這裡 bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied); bulkCopy.WriteToServer(reader); } Target.Close(); } sourceConnection.Close(); reader.Close(); } Console.WriteLine("這次資料匯入共用了:" + st.ElapsedMilliseconds); st.Stop(); } static void bulkCopy_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e) { Console.WriteLine("已經有" + e.RowsCopied + "條資料被匯入"); } } }