Hi guys,
Im trying to create a console app that will read from a text file and import the file into a sql database. I have completed the following already:
Read from a text file and put out to console
Read from sql database and put out to console
Now i need to read from the text file into the database and im stuck:
Ill give you the code I have got already:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
namespace ConsoleApplication1
{
class reports
{
static void Main(string[] args)
{
try
{
//read from textfile
FileStream readfile = new FileStream("c:\\testbench1\\info\\20090225174432", FileMode.Open);
StreamReader streamreader = new StreamReader(readfile);
string line = "";
int lineNo = 0;
do
{
line = streamreader.ReadLine();
if (line != null)
{
Console.WriteLine("{0}: {1}", lineNo, line);
lineNo++;
}
} while (line != null);
}
catch (Exception e)
{
Console.WriteLine("Exception in ShowFile: {0}", e);
Console.Write("Pause\n");
string ServerId = Console.ReadLine();
}
SqlConnection dataConnection = new SqlConnection();
try
{
dataConnection.ConnectionString = "Data Source=chris-laptop\\project;Initial Catalog=sitehealth;Integrated Security=True";
dataConnection.Open();
Console.WriteLine("connected to database");
}
catch (Exception e)
{
Console.WriteLine("error opening the database: " + e.Message);
string customerId = Console.ReadLine();
}
try
{
//read from sql database
Console.Write("please enter a serverID\n");
string ServerId = Console.ReadLine();
SqlCommand dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;
dataCommand.CommandText = "SELECT HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNO, PrimaryIP, PrimaryIP, DNS, NIS, CTS, UTS ";
dataCommand.CommandText += "FROM T_Servers WHERE ServerID='" +
ServerId + "'";
Console.WriteLine("about to execute: {0}\n\n", dataCommand.CommandText);
SqlDataReader datareader = dataCommand.ExecuteReader();
while (datareader.Read())
{
//code to display current row
//int serverID = datareader.GetInt32(0);
String HostName = datareader.GetString(0);
String MachineType = datareader.GetString(1);
String Kernel = datareader.GetString(2);
byte CPUS = datareader.GetByte(3);
String Speed = datareader.GetString(4);
int Memory = datareader.GetInt32(5);
String Arch = datareader.GetString(6);
String SerialNO = datareader.GetString(7);
String PrimaryIP = datareader.GetString(9);
String DNS = datareader.GetString(10);
String NIS = datareader.GetString(11);
DateTime CTS = datareader.GetDateTime(12);
DateTime UTS = datareader.GetDateTime(13);
Console.WriteLine("hostname= {0}\nMqcType= {1}\nKernel={2}\n" +
"CPU's={3}\nCPUSpeed={4}\nMemory={5}\nArch={6}\n" +
"Serial={7}\nPrimary IP={8}\nDNS={9}\nNIS={10}\nCTS={11}\nUTS={12}", HostName,
MachineType, Kernel, CPUS, Speed, Memory, Arch, SerialNO, PrimaryIP,
DNS, NIS, CTS, UTS);
}
datareader.Close();
//add code
string test = Console.ReadLine();
//now we have read from the database lets try to write to
//the database
}
catch (Exception e)
{
Console.WriteLine("error reading from the database: " + e.Message);
string customerId = Console.ReadLine();
}
finally
{
dataConnection.Close();
}
}
}
}
Also here is an example text file:
testbench2
i86pc
Generic_127128-11
2
2790
1024
i386
VMware-56_4d_b2_6e_3a_8b_c2_8a-02_0b_49_06_ed_a1_56_3c
192.168.1.100
DNS
noNIS
And here is the table it will be writing too:
T_Servers
[ServerID] [int] IDENTITY(1,1) NOT NULL,
[HostName] [varchar](50) NOT NULL,
[MachineType] [varchar](50) NOT NULL,
[Kernel] [varchar](50) NOT NULL,
[CPUS] [tinyint] NOT NULL,
[CPUSPeed] [varchar](15) NOT NULL,
[MemoryMB] [int] NOT NULL,
[Architecture] [varchar](50) NOT NULL,
[SerialNo] [varchar](100) NOT NULL,
[PrimaryIP] [varchar](20) NOT NULL,
[DNS] [varchar](10) NOT NULL,
[NIS] [varchar](10) NOT NULL,
[CTS] [datetime] NOT NULL,
[UTS] [datetime] NOT NULL,
I know its a massive ask for help but i really need to be able to read the text file straight to the database, also CTS (current time stamp) and UTS (update time stamp) should be auto generated as will the server ID number, i have looked at various guides but they dont really make sense help!!