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
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
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.
I need to tell when the file is at a new server and insert records for that server.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 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);
}
}
while ((line = sr.ReadLine()) != null)
{
if(line.Contains(":"))
{
splitArr = line.Split(":");
InsertRecord(splitArr[0], splitArr[1]);
}
else //It is a server line
{
InsertServerRecord(line);
}
}
ASKER
The server record needs to be added to each inserted record. i dont believe that logic would work. Thanks for helping me
Open in new window