我正在使用ASP.Net MVC上传.txt文件。我使用了http://www.dotnetcurry.com/showarticle.aspx?ID=323中的代码。当我上传一个超过30,000行的.txt文件时,我收到此错误:输入数组比此表中的列数长。
-
表 TMPUPLOAD 列:
vdate |时间|安培| EMPNO
-
.txt文件的示例数据:
08/01 / 12,05:09:09 AM,200441 ,, North Lobby1,358709,VERIFY_OK
08/01 / 12,05:09:09 AM,200441 ,, North Lobby1,358709,VERIFY_OK
08/22 / 12,12:13:36 PM,228079,Lim,Benedict Drew,South Lobby2,123903,VERIFY_OK
-
和代码:
public ActionResult ChronologFiles()
{
return View();
}
[HttpPost]
public ActionResult ChronologFiles(HttpPostedFileBase FileUpload)
{
DataTable dt = new DataTable();
string line = string.Empty;
int i = 0;
//check we have a file
if (FileUpload.ContentLength > 0)
{
//Workout our file path
string fileName = Path.GetFileName(FileUpload.FileName);
string path = Path.Combine(Server.MapPath("~/App_Data/UploadedFiles"), fileName);
//Try and upload
try
{
FileUpload.SaveAs(path);
//Process the CSV file and capture the results to our DataTable place holder
dt = ProcessCSV(path);
//Process the DataTable and capture the results to our SQL Bulk copy
ViewData["Feedback"] = ProcessBulkCopy(dt);
using (StreamReader sr = new StreamReader(path, true))
{
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(',', ' ', '\n');
if (data.Length > 0)
{
if (i == 0)
{
foreach (var item in data)
{
dt.Columns.Add(new DataColumn());
}
i++;
}
DataRow row = dt.NewRow();
row.ItemArray = data;
dt.Rows.Add(row);
}
}
}
}
catch (Exception ex)
{
//Catch errors
ViewData["Feedback"] = ex.Message;
}
}
else
{
//Catch errors
ViewData["Feedback"] = "Please select a file";
}
//Tidy up
dt.Dispose();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ALEMSDB"].ConnectionString))
{
con.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(con))
{
copy.ColumnMappings.Add(0, "vdate");
copy.ColumnMappings.Add(1, "time");
copy.ColumnMappings.Add(2, "ampm");
copy.ColumnMappings.Add(3, "empno");
copy.DestinationTableName = "TMPUPLOAD";
copy.WriteToServer(dt);
}
using (SqlCommand cmd2 = new SqlCommand("DELETE FROM TMPUPLOAD WHERE empno = ''", con))
{
int rows = cmd2.ExecuteNonQuery();
//rows number of record got inserted
}
using (SqlCommand cmd2 = new SqlCommand(@"INSERT INTO UPLOADING(vdate, [time], ampm, empno)
SELECT vdate, [time], ampm, empno
FROM TMPUPLOAD", con))
{
int rows = cmd2.ExecuteNonQuery();
//rows number of record got inserted
}
using (SqlCommand cmd3 = new SqlCommand(@"UPDATE UPLOADING
SET inout = 'I'
WHERE [time] BETWEEN '04:00:00' AND '11:59:59' OR [time] BETWEEN '12:00:00' AND '16:00:00'
OR [time] BETWEEN '18:00:00' AND '23:59:59' AND ampm = 'AM'", con))
{
int rows = cmd3.ExecuteNonQuery();
//rows number of record got inserted
}
using (SqlCommand cmd4 = new SqlCommand(@"UPDATE UPLOADING
SET inout = 'O'
WHERE [time] BETWEEN '12:00:00' AND '12:59:59' OR [time] BETWEEN '13:00:00' AND '17:59:59'
OR [time] BETWEEN '18:00:00' AND '23:59:59' AND ampm = 'PM'", con))
{
int rows = cmd4.ExecuteNonQuery();
//rows number of record got inserted
}
}
return View();
}
我只需要08/01 / 12,05:09:09 AM,200441这就是为什么我的表中只有四列我真的想知道为什么我会收到这个错误.. 提前致谢! :)
答案 0 :(得分:1)
string[] data = line.Split(',', ' ', '\n');
这行代码将拆分
05:09:09 AM 北大厅1
由于空间,到2列。
答案 1 :(得分:0)
所以你在做:
//Process the CSV file and capture the results to our DataTable place holder
dt = ProcessCSV(path);
其次是:
if (i == 0)
{
// Weren't the columns already added by the ProcessCSV method?
foreach (var item in data)
{
dt.Columns.Add(new DataColumn());
}
i++;
}
如果我理解正确,您已经在ProcessCSV方法中将数据添加到DataTable中。如果DataTable中已包含数据,则DataTable将不允许您添加任何列。
解析CSV通常需要的不仅仅是拆分方法,因为某些字段的格式要求不同。您应该使用built-in parser described here,而不是尝试重新发明轮子来解析CSV。将数据放入单独的CSV字段后,即可在日期字段中进行拆分。