-2

Just need idea of how to read the following format of text file in c#

example:

A B C
D E F
1 2 3

the target table has Column AD, BE, CF, I need populate 1,2,3 to AD,BE,CF column, only space between characters in the text file.

How to do that in c#?

Thanks

LONG
  • 4,490
  • 2
  • 17
  • 35

2 Answers2

0

For ideas on reading the space delimited text file, see this question.

For ideas on inserting a row to SQL Server, see this question.

Community
  • 1
  • 1
Ola Eldøy
  • 5,720
  • 7
  • 49
  • 82
0

Here is a pretty close try at a potential solution for you. Basically as you read in the file line by line you take the 1st two line where your column information is and split on the white space and put the character from the same positions in each line together.

int counter = 0;
string line;
List<string> ColumnHeaders = new List<string>();
// Read the file and display it line by line.
System.IO.StreamReader file = 
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   String[] listOfChars = line.Split(new char[] {' ','\t'}, StringSplitOptions.RemoveEmptyEntries);

   if(counter <2){
        if(ColumnHeaders.count < listOfChars.count){
             ColumnHeaders.addRange(listOfChars);
       } 
       else {
          for( i = 0; i<listOfChars.count; i++){
              ColumnHeaders[i] = ColumnHeaders[i] + listOfChars[i];
           }
       }
    }
    else{
     //run code here to post this line to sql you can use the string split here as well
    }
   counter++;
}

file.Close();
TJM
  • 92
  • 6