如何上传批量CSV文件 [英] How to upload a bulk CSV file

查看:93
本文介绍了如何上传批量CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......

我想使用asp.net c#在sqlserver 2008上传一个140mb的CSV文件。

请帮帮我

*(140mb CSV文件--->我的记录的最大大小)





protected void Button1_Click(对象发送者) ,EventArgs e)

{

//上传并保存文件

string csvPath = Server.MapPath(〜/ Files /) + Path.GetFileName(FileUpload1.PostedFile.FileName);

FileUpload1.SaveAs(csvPath);



DataTable dt = new DataTable() ;

dt.Columns.AddRange(new DataColumn [37]

{

new DataColumn(FirstName,typeof(string)),

new DataColumn(Initial,typeof(string)),

new DataColumn(LastName,typeof(string)),

new DataColumn(AddressLine1,typeof(string)),

new DataColumn(AddressLi ne2,typeof(string)),

new DataColumn(City,typeof(string)),

new DataColumn(State,typeof(string)) ,

新DataColumn(ZIPCode,typeof(string)),

新DataColumn(ZIP4,typeof(string)),

new DataColumn(DeliveryPoint,typeof(string)),

new DataColumn(CarrierRoute,typeof(string)),

new DataColumn(CountyNumber, typeof(string)),

new DataColumn(CountyName,typeof(string)),

new DataColumn(Latitude,typeof(string)),

new DataColumn(Longitude,typeof(string)),

new DataColumn(FirstInHousehold,typeof(string)),

new DataColumn (ChildPresent,typeof(string)),

new DataColumn(MFDU,typeof(string)),

new DataColumn( ExactAge,typeof(string)),

new DataColumn(EstAge,typeof(string)),

new DataColumn(EstIncome,typeof(string) ),

new DataColumn(LengthOfResidence,typeof(string)),

new DataColumn(AddrType,typeof(string)),

new DataColumn(DwellingType,typeof(string)),

new DataColumn(HomeownerType,typeof(string)),

new DataColumn(Property) ,typeof(string)),

new DataColumn(MedHomeVL,typeof(string)),

new DataColumn(Marital,typeof(string)),

new DataColumn(EthnicCode,typeof(string)),

new DataColumn(Title,typeof(string)),

new DataColumn(MedianYrsInSchool,typeof(string)),

new DataColumn(Gender,typeof(string)),

new DataColumn(DPVCode,typeof(string)),

new DataColumn(EstWealth,typeof(string)),

new DataColumn(PhoneNumber,typeof (string)),

new DataColumn(TimeZone,typeof(string)),

new DataColumn(Birthdate,typeof(string)),



});

string csvData = File.ReadAllText(csvPath);

foreach(csvData.Split中的字符串行) ('\ n'))

{

if(!string.IsNullOrEmpty(row))

{

dt.Rows.Add();

int i = 0;

foreach(row.Split(',')中的字符串单元格)

{

dt.Rows [dt.Rows.Count - 1] [i] = cell;

i ++;

} < br $>
}

}



string consString = ConfigurationManager.ConnectionStrings [conString]。ConnectionString;

使用(SqlConnection con = new SqlConnection(consString))

{

using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))

{

//设置数据库表名

sqlBulkCopy.DestinationTableName =dbo.tbl_upload ;

con.Open();

sqlBulkCopy.WriteToServer(dt);

con.Close();

}

}

}



i这样做了

这个正在工作

但是在大文件的情况下...时间结束

Hello guys....
I want to upload a CSV file of 140mb in sqlserver 2008 using asp.net c#.
Please help me
*(140mb CSV file---> maximum size of my records)


protected void Button1_Click(object sender, EventArgs e)
{
//Upload and save the file
string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(csvPath);

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[37]
{
new DataColumn("FirstName",typeof(string)),
new DataColumn("Initial", typeof(string)),
new DataColumn("LastName",typeof(string)),
new DataColumn("AddressLine1",typeof(string)) ,
new DataColumn("AddressLine2",typeof(string)) ,
new DataColumn("City",typeof(string)),
new DataColumn("State",typeof(string)) ,
new DataColumn("ZIPCode",typeof(string)) ,
new DataColumn("ZIP4",typeof(string)) ,
new DataColumn("DeliveryPoint",typeof(string)) ,
new DataColumn("CarrierRoute",typeof(string)) ,
new DataColumn("CountyNumber",typeof(string)) ,
new DataColumn("CountyName",typeof(string)) ,
new DataColumn("Latitude",typeof(string)) ,
new DataColumn("Longitude",typeof(string)) ,
new DataColumn("FirstInHousehold",typeof(string)) ,
new DataColumn("ChildPresent",typeof(string)) ,
new DataColumn("MFDU",typeof(string)) ,
new DataColumn("ExactAge",typeof(string)),
new DataColumn("EstAge",typeof(string)),
new DataColumn("EstIncome",typeof(string)) ,
new DataColumn("LengthOfResidence",typeof(string)) ,
new DataColumn("AddrType",typeof(string)) ,
new DataColumn("DwellingType",typeof(string)) ,
new DataColumn("HomeownerType",typeof(string)) ,
new DataColumn("Property",typeof(string)),
new DataColumn("MedHomeVL",typeof(string)) ,
new DataColumn("Marital",typeof(string)) ,
new DataColumn("EthnicCode",typeof(string)) ,
new DataColumn("Title",typeof(string)) ,
new DataColumn("MedianYrsInSchool",typeof(string)) ,
new DataColumn("Gender",typeof(string)),
new DataColumn("DPVCode",typeof(string)) ,
new DataColumn("EstWealth",typeof(string)) ,
new DataColumn("PhoneNumber",typeof(string)) ,
new DataColumn("TimeZone",typeof(string)),
new DataColumn("Birthdate",typeof(string)),

});
string csvData = File.ReadAllText(csvPath);
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}

string consString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.tbl_upload";
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}

i have done this
this is working
but in case of large files...the times goes out

推荐答案

问题是你正在将所有内容加载到内存中(数据表)。我建议您使用和现有的解决方案来解析/批量插入CSV数据,而不是重新发明轮子。一个好的候选人似乎是 LumenWorks CSV阅读器 [ ^ ]。我相信这个 [ ^ ]正是您所需要的。
The problem is you are loading everything into memory (the data table). I would suggest you use and existing solution for parsing/bulk-inserting CSV data and not to reinvent the wheel. One good candidate seems to be LumenWorks CSV Reader[^]. I believe this[^] is exactly what you need.


这篇关于如何上传批量CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆