0

I have approx. 150 text files containing data I need to insert into MS SQL tables. As I read the lines I read the first 4 characters to determine the data in that line. The line following the current line may or may not be data relating to the current line (I can only know this once I read the line as there are no linking identifiers). The only way to tell the next line is a new record is to read the line code. Here is an example of the data (it is data read from a cash register). I have commented each line to explain what it is.

0001,00,111008,05,0909,000702,0003,0000,0001,0000,0000,0000,0000,2 //0001 = Sales header**
1C00 //1C00 = No Sale (and relates to the Sales Header above)
0001,00,111008,05,0934,000702,0004,0000,0001,0000,0000,0000,0000,2 //0001 = Sales header
1C00 //1C00 = No Sale (and relates to the Sales Header above)
0001,00,111008,05,0945,000702,0005,0000,0001,0000,8000,0000,0000,4 //0001 = Sales header
0200,1,19781849813891,1.000,5.99,5.99,000000,00,00,01,10,10 //0200 = Sales Detail line 
1401,5.99,0000,000000,00 //1401 = Card Sale (operator press wrong button as its followed by change given record)
1501,0.00,02,01,0000,000000,00 //1501 - Change Given
0001,00,111008,00,1327,000802,0103,0000,0003,0000,8000,0000,1000,7 //0001 = Sales header
0200,1,19780857223968,1.000,7.99,7.99,000000,00,00,01,10,10 //0200 = Sales Detail line 
0200,1,19781847708427,1.000,7.99,7.99,000000,00,00,01,10,10 //0200 = Sales Detail line  
0502,-10.00,-1.60,00,FF //0502 - Discount applied to complete sale
5101,14.38,2.40,11.98 //5101 - Sub Total after discount applied
1201,15.00,0000,000000,00 //1201 - Cash Sale
1501,0.62,00,01,0000,000000,00 //1501 - Change Given

My plan is to create a schema and use EF to insert the data as it will sort the linking ids for me. Or I could create my own classes and assign the ids myself. The one requirement that is creating a problem for me is that the SaleHeader record must have a "No Sale" flag, seems simple enough and probably is...

However, either way, my problem comes when reading the text files....

        foreach (string file in Directory.GetFiles(@"C:\Temp2\Tills\"))
        {
            foreach (string line in File.ReadLines(@file))
            {

                string[] linearr = line.Split(',');

I need to know what data is on the next line at this point.

                if(linearr[0] == "0001")
                {
                    //New sale
                }
                else if (linearr[0] == "0200")
                {
                    //Sale details line
                }
                else if (linearr[0] == "0502")
                {
                    //Sale discount line
                }


            }
       }

I could use File.ReadAllLines to read all the lines into a string array which will make it easy to access the next line. some of these files are fairly big so processing them line by line without having to read the whole file first.

The question is: Is there anyway to "Peek Ahead" at the next line while reading files in line by line?

If anyone can see a simpler was of getting this data into a database I would be interested to hear!

Fred
  • 5,663
  • 4
  • 45
  • 74
  • 2
    take a look at [this answer](http://stackoverflow.com/questions/842465/reading-a-line-from-a-streamreader-without-consuming) or [this answer](http://stackoverflow.com/questions/13548443/textreader-and-peek-next-line) – Jonesopolis Nov 21 '14 at 15:20
  • Alternatively you could remember the previous line. – juharr Nov 21 '14 at 15:30

1 Answers1

1

In case this is found from a Google search...

I changed my approach and went with ReadAllLines. I read the whole file into a string array and then split each line to get the values. I set the Id's myself and if I find the value I am looking for I set the previous headerline "no sale" value. I then load all the data into a data table and use SqlBulkCopy to insert the data into the database. I reduced the time taken by an existing app from 18 hours to process 38 files to 18 mins to process 170 files. Happy with that.

Fred
  • 5,663
  • 4
  • 45
  • 74