?
Solved

C# console app

Posted on 2011-10-12
6
Medium Priority
?
382 Views
Last Modified: 2013-12-16
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
Comment
Question by:wademi
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36958112
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
 

Author Comment

by:wademi
ID: 36958182
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
 
LVL 28

Accepted Solution

by:
strickdd earned 2000 total points
ID: 36958207
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 

Author Comment

by:wademi
ID: 36958277
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
 
LVL 28

Expert Comment

by:strickdd
ID: 36958315
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
 

Author Comment

by:wademi
ID: 36958464
The server record needs to be added to each inserted record. i dont believe that logic would work.  Thanks for helping me
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question