943,729 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 6780
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 28th, 2009
0

import txt file sql database plz help!

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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!!
Similar Threads
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Feb 28th, 2009
0

Re: import txt file sql database plz help!

Have you worked out the SQL you would need to run to put the data into your table?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 28th, 2009
0

Re: import txt file sql database plz help!

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

C# Syntax (Toggle Plain Text)
  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!
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 1st, 2009
0

Re: import txt file sql database plz help!

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 1st, 2009
0

Re: import txt file sql database plz help!

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??
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 1st, 2009
0

Re: import txt file sql database plz help!

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!!
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 1st, 2009
1

Re: import txt file sql database plz help!

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 1st, 2009
0

Re: import txt file sql database plz help!

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:
C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 1st, 2009
0

Re: import txt file sql database plz help!

OK do you know what "Scope" is?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 1st, 2009
0

Re: import txt file sql database plz help!

hmm have heard of it but dont really know too much about it!
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: using delegates help
Next Thread in C# Forum Timeline: Help with AxInterop OCW11





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC