• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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
0
wademi
Asked:
wademi
  • 3
  • 3
1 Solution
 
strickddCommented:
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

0
 
wademiAuthor Commented:
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.
0
 
strickddCommented:
That's in this part here:

while ((line = sr.ReadLine()) != null) 
                {
                    splitArr = line.Split(":");
                    InsertRecord(splitArr[0], splitArr[1]);
                }

Open in new window


This reads each line, splits it on the ":" and sends the server and Started/Stopped text to the InsertRecord() function. This function will need to contain your SQL logic to insert into the database.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
wademiAuthor Commented:
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.
0
 
strickddCommented:
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);
                     }
                }
0
 
wademiAuthor Commented:
The server record needs to be added to each inserted record. i dont believe that logic would work.  Thanks for helping me
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now