百万条数据批量导入

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();
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值