1. 程式人生 > >百萬條資料批量匯入

百萬條資料批量匯入

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


namespace DataImport{
    class Program
    {
        static void Main(string[] args)
        {  
            //資料庫連線物件
            string connStr = "Data Source=伺服器IP;Initial Catalog=資料庫名;UID=使用者名稱;PWD=密碼";
            //匯入資料的檔案路徑
            string path = @"絕對路徑";
            //計時方法
            Stopwatch sw = new Stopwatch();
            sw.Start();
            using (StreamReader reader = new StreamReader(path))
            {
                string str = string.Empty;

                DataTable dt = new DataTable();
                dt.Columns.Add("CALLERNO", typeof(string));
                dt.Columns.Add("CALLSTARTTIME", typeof(string));
                dt.Columns.Add("SERIALNO", typeof(string));
                dt.Columns.Add("CALLDURATION", typeof(string));
                int n = 0;
                while (!string.IsNullOrEmpty((str = reader.ReadLine())))
                {
                    n++;
                    string[] arr = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (arr.Length == 0)
                    {
                        break;
                    }
                    string CALLERNO = arr[0];
                    string CALLSTARTTIME = arr[1];
                    string SERIALNO = arr[2];
                    string CALLDURATION = arr[3];
                    DataRow row = dt.NewRow();
                    row["CALLERNO"] = CALLERNO;
                    row["CALLSTARTTIME"] = CALLSTARTTIME;
                    row["SERIALNO"] = SERIALNO;
                    row["CALLDURATION"] = CALLDURATION;
                    dt.Rows.Add(row);
                   
                }
                Console.WriteLine("Lines=" + n);
             
               
               
                using (SqlBulkCopy copy = new SqlBulkCopy(connStr))
                {
                    copy.DestinationTableName = "Sixcallout";//資料庫接受資料的資料表
                    copy.BatchSize = 1000000;//每次匯入資料行數
                    copy.BulkCopyTimeout = 3600;
                    //建立對映這步很關鍵
                    copy.ColumnMappings.Add("CALLERNO", "CALLERNO");
                    copy.ColumnMappings.Add("CALLSTARTTIME", "CALLSTARTTIME");
                    copy.ColumnMappings.Add("SERIALNO", "SERIALNO");
                    copy.ColumnMappings.Add("CALLDURATION", "CALLDURATION");
                    copy.WriteToServer(dt);
                }
                sw.Stop();
                Console.WriteLine("錄入完畢!耗時:" + sw.ElapsedMilliseconds);
            }
            Console.ReadKey();
        }

    }
}