I am importing .csv file in my sql database. I am using TextFieldParser.
My code is
TextFieldParser parser = new TextFieldParser(file);
//single file
//TextFieldParser parser = new TextFieldParser(CSVFolderPath + "\\" + file);
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
int k = 0;
while (!parser.EndOfData)
{
//Processing row
string[] fields = parser.ReadFields();
if (k != 0)
{
for (int i = 0; i < fields.Length; i++)
{
stationcode = fields[0].ToString().Substring(4, 5);
//if (fields[1].ToString().Substring(14, 8) == date)
//{
if (i == 0)
{
dr = workTable.NewRow();
dr[i] = fields[i].Substring(0, fields[i].Length - 4);
}
else if (i == 3)
{
dr[i] = Convert.ToDateTime(fields[i].ToString());
}
else if (i == 4)
{
dr[i] = Convert.ToDateTime(fields[i].ToString());
}
else if (i == 5)
{
dr[i] = Convert.ToInt32(fields[i].ToString());
}
else
dr[i] = fields[i].ToString();
if (i == 5)
{
workTable.Rows.Add(dr);
}
//}
}
}
k = k + 1;
}
parser.Close();
Here worktable is DataTable.
Code parse file fine.
But in my csv file last line is used for summary. Total of some fields.
I dont want to include that line for inserting in datatable.
How can I do this ?
If memory space won’t be an issue. Load the list of CSV lines first, and parse each line save the last. Otherwise you need some way of identifying summary rows. (such as null data for things like dates) which can signal the parser to ignore that row.
The best way to deal with data for import is to try and establish that import files will only include data to be imported. (No headers, no summaries, etc.) Unfortunately a lot of companies try importing Reports instead of actual export files.