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