Skip to content Skip to sidebar Skip to footer

Textfieldparser With Not Parsing Last Line

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 =

Solution 1:

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.

Solution 2:

Solution 3:

If you have many different cases required for identifying the summary row & memory space IS an issue, you can always use the TextFieldParser.PeekChars(Int32) method:

Reads the specified number of characters without advancing the cursor.

In the remarks section:

The numberOfChars value must be less than the total number of characters in the line. If it is not, the string returned by PeekChars will be truncated to the length of the line.

Therefor:

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

    //Peek if this is the last line, then breakif (parser.PeekChars(Int32.MaxValue) is"") break;

    if (k != 0) 
    { 
...

docs.microsoft link

Post a Comment for "Textfieldparser With Not Parsing Last Line"