C #, reading fixed-width records, changing record types in a single file

To get started, I would like to clarify that I'm not very good at C #. In this project that I am running in C # using .Net 3.5, I need to create a class for reading and exporting files that contain several fixed-width formats based on the type of record.

Currently, there are 5 types of records indicated by the first character position in each line of the file indicating a specific line format. The problem is that types are different from each other.

Record type 1 has 5 columns, signifies beginning of the file

Record type 3 has 10 columns, signifies beginning of a batch
Record type 5 has 69 columns, signifies a transaction
Record type 7 has 12 columns, signifies end of the batch, summarizes
(these 3 repeat throughout the file to contain each batch)

Record type 9 has 8 columns, signifies end of the file, summarizes

Is there a good library for these fixed-width files? I saw several good ones who want to download the entire file as a single specification, but this will not be done.

250 , 300 . .

, , "", ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Extract_Processing
{
    class Extract
    {
        private string mFilePath;
        private string mFileName;
        private FileHeader mFileHeader;
        private FileTrailer mFileTrailer;
        private List<Batch> mBatches;       // A file can have many batches

        public Extract(string filePath)
        { /* Using file path some static method from another class would be called to parse in the file somehow */ }

        public string ToString()
        { /* Iterates all objects down the heiarchy to return the file in string format */ }

        public void ToFile()
        { /* Calls some method in the file parse static class to export the file back to storage somewhere */ }
    }

    class FileHeader
    { /* ... contains data types for all fields in this format, ToString etc */ }

    class Batch
    {
        private string mBatchNumber;                // Should this be pulled out of the batch header to make LINQ querying simpler for this data set?
        private BatchHeader mBatchHeader;
        private BatchTrailer mBatchTrailer;
        private List<Transaction> mTransactions;    // A batch can have multiple transactions

        public string ToString()
        { /* Iterates through batches to return what the entire batch would look like in string format */ }
    }

    class BatchHeader
    { /* ... contains data types for all fields in this format, ToString etc */ }

    class Transaction
    { /* ... contains data types for all fields in this format, ToString etc */ }

    class BatchTrailer
    { /* ... contains data types for all fields in this format, ToString etc */ }

    class FileTrailer
    { /* ... contains data types for all fields in this format, ToString etc */ }

}

Ive , , . , , #, .

, , ? , VBA, FSO, Microsoft Access ImportSpec (5 , spec... wow, !), "" visual foxpro ( FAAAAAAAST, , ), #, , .

, , . , , , .

+3
4

, - , - ?

, .

StreamReader 64 - ( 1500 ).

:
1) Read , .
2) ReadLine String.Split, .
3) , .

IndexOf + SubString ( ).

, , , BinaryReader .

+2

FileHelpers - . , , , , , . .

? SQL Server? , FAST SIMPLE, :

  • , 5 . LineNumber FileName, .
  • - DataTable ADO.NET, .
  • -, -, DataTable, .
  • DataTable BatchSize (, 1000 ), SqlBulkCopy . SqlBulkCopy DataTable .
  • -, SQL Server.

, 500 #.

+4

, , , ToString.

    public string ToString()

:

    public override string ToString()
+1
-1

Source: https://habr.com/ru/post/1752979/


All Articles