Link to home
Start Free TrialLog in
Avatar of wademi
wademi

asked on

C# console app

I would like to scan a text file using a C# console application and populate my SQL table with data from the  text file.

Can someone help me with the C# script to do this?

My text file looks like this

547822-WEB1-Server
Mytestsite.com:Started
Fastsite.com:Stopped
598617-WEB15 - Server
bigsite.com:Started
little.com:Started
blussite:Stopped

My Table looks like this
Id,Server,Site,State
The end result should be like this.

ID    Server                                  Site                                   State
1     547822-WEB1-Server       Mytestsite.com              Started
2     547822-WEB1-Server       Fastsite.com                   Stopped
3     598617-WEB15 – Server  bigsite.com                     Started

And so on……………..

The column ID is an auto generated GUID
Avatar of strickdd
strickdd
Flag of United States of America image

How is the text file generated? If you have control over that you could convert it to a format that is compatible with LINQ and query it directly, otherwise, you will need to manually loop through the line with a stream reader.

try 
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;
                string[] splitArr;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null) 
                {
                    splitArr = line.Split(":");
                    InsertRecord(splitArr[0], splitArr[1]);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }

Open in new window

Avatar of wademi
wademi

ASKER

That looks fine, what about the server insert logic.
I need to tell when the file is at a new server and insert records for that server.
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wademi

ASKER

Split only takes care of the site and state. What about the server?
 I am also getting an The file could not be read:
Index was outside the bounds of the array.
I'm sorry, I totally missed that. Simple change:

while ((line = sr.ReadLine()) != null)
                {
                    if(line.Contains(":"))
                    {
                    splitArr = line.Split(":");
                    InsertRecord(splitArr[0], splitArr[1]);
                    }
                    else //It is a server line
                    {
                        InsertServerRecord(line);
                     }
                }
Avatar of wademi

ASKER

The server record needs to be added to each inserted record. i dont believe that logic would work.  Thanks for helping me