import txt file sql database plz help!

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

import txt file sql database plz help!

 
0
  #1
Feb 28th, 2009
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:

  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Data.SqlClient;
  7. using System.Data.Sql;
  8. using System.Data.SqlTypes;
  9.  
  10.  
  11. namespace ConsoleApplication1
  12. {
  13. class reports
  14. {
  15. static void Main(string[] args)
  16. {
  17. try
  18. {
  19. //read from textfile
  20. FileStream readfile = new FileStream("c:\\testbench1\\info\\20090225174432", FileMode.Open);
  21. StreamReader streamreader = new StreamReader(readfile);
  22.  
  23. string line = "";
  24.  
  25. int lineNo = 0;
  26. do
  27. {
  28. line = streamreader.ReadLine();
  29. if (line != null)
  30. {
  31. Console.WriteLine("{0}: {1}", lineNo, line);
  32. lineNo++;
  33. }
  34. } while (line != null);
  35.  
  36. }
  37. catch (Exception e)
  38. {
  39. Console.WriteLine("Exception in ShowFile: {0}", e);
  40. Console.Write("Pause\n");
  41. string ServerId = Console.ReadLine();
  42. }
  43.  
  44. SqlConnection dataConnection = new SqlConnection();
  45. try
  46. {
  47. dataConnection.ConnectionString = "Data Source=chris-laptop\\project;Initial Catalog=sitehealth;Integrated Security=True";
  48. dataConnection.Open();
  49. Console.WriteLine("connected to database");
  50. }
  51.  
  52. catch (Exception e)
  53. {
  54. Console.WriteLine("error opening the database: " + e.Message);
  55. string customerId = Console.ReadLine();
  56. }
  57. try
  58. {
  59. //read from sql database
  60. Console.Write("please enter a serverID\n");
  61. string ServerId = Console.ReadLine();
  62. SqlCommand dataCommand = new SqlCommand();
  63. dataCommand.Connection = dataConnection;
  64. dataCommand.CommandText = "SELECT HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNO, PrimaryIP, PrimaryIP, DNS, NIS, CTS, UTS ";
  65. dataCommand.CommandText += "FROM T_Servers WHERE ServerID='" +
  66. ServerId + "'";
  67. Console.WriteLine("about to execute: {0}\n\n", dataCommand.CommandText);
  68. SqlDataReader datareader = dataCommand.ExecuteReader();
  69.  
  70. while (datareader.Read())
  71. {
  72. //code to display current row
  73. //int serverID = datareader.GetInt32(0);
  74. String HostName = datareader.GetString(0);
  75. String MachineType = datareader.GetString(1);
  76. String Kernel = datareader.GetString(2);
  77. byte CPUS = datareader.GetByte(3);
  78. String Speed = datareader.GetString(4);
  79. int Memory = datareader.GetInt32(5);
  80. String Arch = datareader.GetString(6);
  81. String SerialNO = datareader.GetString(7);
  82. String PrimaryIP = datareader.GetString(9);
  83. String DNS = datareader.GetString(10);
  84. String NIS = datareader.GetString(11);
  85. DateTime CTS = datareader.GetDateTime(12);
  86. DateTime UTS = datareader.GetDateTime(13);
  87.  
  88.  
  89. Console.WriteLine("hostname= {0}\nMqcType= {1}\nKernel={2}\n" +
  90. "CPU's={3}\nCPUSpeed={4}\nMemory={5}\nArch={6}\n" +
  91. "Serial={7}\nPrimary IP={8}\nDNS={9}\nNIS={10}\nCTS={11}\nUTS={12}", HostName,
  92. MachineType, Kernel, CPUS, Speed, Memory, Arch, SerialNO, PrimaryIP,
  93. DNS, NIS, CTS, UTS);
  94.  
  95. }
  96. datareader.Close();
  97. //add code
  98. string test = Console.ReadLine();
  99. //now we have read from the database lets try to write to
  100. //the database
  101. }
  102. catch (Exception e)
  103. {
  104. Console.WriteLine("error reading from the database: " + e.Message);
  105. string customerId = Console.ReadLine();
  106. }
  107. finally
  108. {
  109. dataConnection.Close();
  110. }
  111.  
  112. }
  113. }
  114. }

Also here is an example text file:

  1. testbench2
  2. i86pc
  3. Generic_127128-11
  4. 2
  5. 2790
  6. 1024
  7. i386
  8. VMware-56_4d_b2_6e_3a_8b_c2_8a-02_0b_49_06_ed_a1_56_3c
  9. 192.168.1.100
  10. DNS
  11. noNIS

And here is the table it will be writing too:

  1. T_Servers
  2. [ServerID] [int] IDENTITY(1,1) NOT NULL,
  3. [HostName] [varchar](50) NOT NULL,
  4. [MachineType] [varchar](50) NOT NULL,
  5. [Kernel] [varchar](50) NOT NULL,
  6. [CPUS] [tinyint] NOT NULL,
  7. [CPUSPeed] [varchar](15) NOT NULL,
  8. [MemoryMB] [int] NOT NULL,
  9. [Architecture] [varchar](50) NOT NULL,
  10. [SerialNo] [varchar](100) NOT NULL,
  11. [PrimaryIP] [varchar](20) NOT NULL,
  12. [DNS] [varchar](10) NOT NULL,
  13. [NIS] [varchar](10) NOT NULL,
  14. [CTS] [datetime] NOT NULL,
  15. [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!!
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: import txt file sql database plz help!

 
0
  #2
Feb 28th, 2009
Have you worked out the SQL you would need to run to put the data into your table?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: import txt file sql database plz help!

 
0
  #3
Feb 28th, 2009
kinda and kinda not!
This is what i do know!
I know i need to read the text file line by line and insert it into the database but obviously there declared as different types so don’t know whether i cld do it as one command or whether i wld need to read each line into an array or a variable, once the data was in memory i wld need to write it to the database i know the sql statement looks like: im assuming im reading the text file into variables named the same at the columns

  1. INSERT INTO T_Servers(HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNo, PrimaryIP, DNS,
  2. NIS)
  3. VALUES (HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNo, PrimaryIP, DNS,
  4. NIS)

The serverID column UTS and CTS should auto update, and I’m pretty sure that SQL command is right but this is the first time i have done any SQL ever!
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: import txt file sql database plz help!

 
0
  #4
Mar 1st, 2009
So. Each line of your file has a field, hopefully you can work out how to read the file into variables, then now you have an template for how to do the SQL, you need to work out how to add those values. As the values as they are in your post wont take the values from the variables.

But you're getting there.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: import txt file sql database plz help!

 
0
  #5
Mar 1st, 2009
Yer the SQL stuff and reading the file isnt too bad its the method of actually writing to the database Im stuck on any help??
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: import txt file sql database plz help!

 
0
  #6
Mar 1st, 2009
hello again,

Right i have now done what i needed to do, i can read from a text file into a sql database!! (code below). Have lots more things to do but for now this is it cheers!!
  1. try
  2. {
  3. StreamReader objReader = new StreamReader("c:\\testbench1\\info\\20090225174432");
  4. string sLine = "";
  5. ArrayList arrText = new ArrayList();
  6.  
  7. while (sLine != null)
  8. {
  9. sLine = objReader.ReadLine();
  10. if (sLine != null)
  11. arrText.Add(sLine);
  12. }
  13. objReader.Close();
  14. Console.WriteLine(arrText[0]);
  15. Console.ReadLine();
  16. SqlCommand dataCommand = new SqlCommand();
  17. dataCommand.Connection = dataConnection;
  18. dataCommand.CommandText = "INSERT INTO T_Servers(HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNo, PrimaryIP, DNS,NIS)";
  19. dataCommand.CommandText += "VALUES ('" + arrText[0] + "','" + arrText[1] + "','" + arrText[2] + "'," + arrText[3] + ",'" + arrText[4] + "',";
  20. dataCommand.CommandText += "" + arrText[5] + ",'" + arrText[6] + "','" + arrText[7] + "','" + arrText[8] + "','" + arrText[9] + "','" + arrText[10] + "')";
  21. Console.WriteLine("about to execute: {0}\n\n", dataCommand.CommandText);
  22. Console.ReadLine();
  23. SqlDataReader datareader = dataCommand.ExecuteReader();
  24. datareader.Close();
  25. }
  26. catch (Exception e)
  27. {
  28. Console.WriteLine("Exception in ShowFile: {0}", e);
  29. Console.Write("Pause\n");
  30. Console.ReadLine();
  31. }
Last edited by chris5126; Mar 1st, 2009 at 7:29 am.
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: import txt file sql database plz help!

 
1
  #7
Mar 1st, 2009
Well done. And whats better is is you almost certainly understand why you ended up with thec ode you did, rather than have someone hand you it.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: import txt file sql database plz help!

 
0
  #8
Mar 1st, 2009
hi liz,

thank you for you usefull advice i need one more bit of help if thats ok:

My code is below but i need to put certain aspects into functions. e.g having the following in a function would be very usefull as it will be called multiple times:
  1. SqlConnection dataConnection = new SqlConnection();
  2. try
  3. {
  4. dataConnection.ConnectionString = "Data Source=chris-laptop\\project;Initial Catalog=sitehealth;Integrated Security=True";
  5. dataConnection.Open();
  6. Console.WriteLine("connected to database");
  7. }
  8. //if it doesn't work show an error
  9. catch (Exception e)
  10. {
  11. Console.WriteLine("error opening the database: " + e.Message);
  12. string customerId = Console.ReadLine();
  13. }
First question where does this fuction go? is it at the start of main or in the class or namespace and what what i need to pass in and pass out. As when i tried to make it into a function:

  1. static void openConnection ()
  2. {
  3. //open connection to database
  4. SqlConnection dataConnection = new SqlConnection();
  5. try
  6. {
  7. dataConnection.ConnectionString = "Data Source=chris-laptop\\project;Initial Catalog=sitehealth;Integrated Security=True";
  8. dataConnection.Open();
  9. Console.WriteLine("connected to database");
  10. }
  11. //if it doesn't work show an error
  12. catch (Exception e)
  13. {
  14. Console.WriteLine("error opening the database: " + e.Message);
  15. string customerId = Console.ReadLine();
  16. }
  17. }
I get a load of errors have been looking for ages
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: import txt file sql database plz help!

 
0
  #9
Mar 1st, 2009
OK do you know what "Scope" is?
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: import txt file sql database plz help!

 
0
  #10
Mar 1st, 2009
hmm have heard of it but dont really know too much about it!
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC