大数据量.csv文件导入SQLServer数据库
时间:2021-07-01 10:21:17
帮助过:12人阅读
void Main(
string[] args)
{
int count =
0;
string readerPath=
@"C:\Users\Administrator\Desktop\readerDemo.csv";
string writerPath=
@"C:\Users\Administrator\Desktop\writeDemo.csv";
if (File.Exists(writerPath))
{
File.Delete(writerPath);
}
using (StreamReader reader =
new StreamReader(readerPath,Encoding.UTF8))
{
while (!
reader.EndOfStream)
{
string line =
reader.ReadLine();
using (StreamWriter writer =
new StreamWriter(writerPath,
true,Encoding.Default))
{
writer.WriteLine(line);
}
count++
;
Console.WriteLine("正在转码第{0}行,请稍等",count);
}
}
Console.WriteLine("转码完成,共转码{0}条数据",count);
Console.WriteLine("开始导入数据,请稍等");
string sql =
"BULK INSERT Test.dbo.BagDataTable FROM ‘C:\\Users\\Administrator\\Desktop\\writeDemo.csv‘ WITH(FIELDTERMINATOR=‘,‘,BATCHSIZE=100000,FIRSTROW=2)";
try
{
DbHelper.ExecuteSql(sql);
}
catch(Exception ex)
{
using (StreamWriter writerLog =
new StreamWriter(
@"C:\Users\Administrator\Desktop\Log.txt"))
{
writerLog.WriteLine(ex.ToString());
}
}
Console.WriteLine("数据导入完毕");
Console.ReadKey();
}
}
Bulk Insert命令详细:
http://blog.csdn.net/jackmacro/article/details/5959321/
FIELDTERMINATOR表示csv文件中的结束符标志(csv默认列结束标志是,行结束标志是\r\n),具体使用参看:
http://www.cnblogs.com/sunice/p/6367332.html
大数据量.csv文件导入SQLServer数据库
标签:默认 dem users htm oid str cep art demo