views:

794

answers:

1

How to Read Schema of Flat File using Reflection in C# The brief overview that I have is- There's some server which is storing the data as flat files and I have to push data from SQL Server to these flat files. For that I need to know their schema. I have no idea about this, any help would be great.

+2  A: 

Hard to know where to start with this question, because it sounds like reflection is unlikely to be helpful, and yet you specified that it must appear in the solution! :)

As a result, this answer is mostly guesswork and will probably evolve as you reveal more information. This is fine; you can't get the answer (42) until you know what the question is.

There isn't a single definition of what a flat file is. What you probably need is a function that parses the "records" in the file. The usual approach is to open the file in a hex editor and infer the format from a few example records. Look for clues as to the shape of the repeating pattern. Does each "record" appear to consist of a number of "fields"? Do they appear in a definite order every time, or is each field prefixed by a name or number to indicate what it means? Does it look like plain text, i.e. can you open it in Notepad without losing important information?

Declare a class to represent a record:

public class FlatFileRecord
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // etc.
}

Then write a function that reads from a stream and yields records:

public static IEnumerable<FlatFileRecord> ParseFlatFile(StreamReader stream)
{
    // etc.

It would be appropriate to use StreamReader if the file is "plain text" (that is, it has carriage-return/line-feed characters that are used in some significant way to delimit records or fields).

Suppose each record is a line of plain text (in other words, each record is separated by a special line-ending sequence). Within that line there are multiple fields, somehow delimited by other special characters. Your parser might be structured as a loop:

string line;
while ((line = r.ReadLine()) != null)
    yield return new FlatFileRecord(line);

This breaks the problem down into two levels. The outer level goes through the lines one at a time, each producing one record. Then there is an inner level that I've imagined putting in the constructor of FlatFileRecord. Pass a line, and it fills itself in with the field values that it knows how to extract from that line. So now you have to write the constructore of FlatFileRecord.

Daniel Earwicker