2 Replies - 4376 Views - Last Post: 07 November 2011 - 04:56 PM Rate Topic: -----

#1 roguex20   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 11-August 11

Where to start? Import data from multiple files into sql via c#

Posted 07 November 2011 - 04:20 PM

I think I am overwhelmed. Studying sql and c# by using different sources. However I think I need put c# and sql to use as I am getting tired of just reading and doing the small exercises. I haven't found an exercise that tackles the following.

I have multiple files I need to import data from. Jumping around on the web, reading tutorials etc - hasn't helped me actually get started.

Can someone lay out a map for me to follow. Not write the code, but give me the best known layout(if one exist).

The txt files are in a folder. The txt files have x amount of fields separated by "|". Each txt file would represent a record and the fields in the txt need to be imported into the corresponding column.

Is This A Good Question/Topic? 0
  • +

Replies To: Where to start? Import data from multiple files into sql via c#

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5509
  • View blog
  • Posts: 14,497
  • Joined: 18-April 07

Re: Where to start? Import data from multiple files into sql via c#

Posted 07 November 2011 - 04:44 PM

Well it sounds like you need to simply loop through each file, read each line, break those lines on the "|" and do an insert with them until you each the end of the file. Then you can move onto the next.

So start by reading all the files you need to from the directory. You can do this with something along the lines of...

String[] someFiles = Directory.GetFiles("c:\\directory_name_here");

foreach (String aFile in someFiles) {
    
}



The Directory.GetFiles will require you to use the System.IO namespace. It comes with an overload version that can take a file pattern if need be as well. So check that piece out.

Then in the foreach loop, you need to...

1) Open a file
2) Read each line until End of file
3) For each line, break it on "|" (look at using a split method from the string class)
4) Create an insert that takes each value and puts it into the correct column. (Make sure to escape it properly)
5) Close file when done
6) Move to next file.


Now normally something like this could have been done with a direct database import. I am assuming you have to do it using a programming method. If I am wrong, then look into database import and use the pipe ("|") as the separator

:)

This post has been edited by Martyr2: 07 November 2011 - 04:45 PM

Was This Post Helpful? 1
  • +
  • -

#3 roguex20   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 11-August 11

Re: Where to start? Import data from multiple files into sql via c#

Posted 07 November 2011 - 04:56 PM

I am almost sure it can be done with just sql, but I started out learning c# - so I want to finally put it to some good use. This will help refuel me to get back to learning more - once I've done something that is needed.

I appreciate the help! Going through your recommendations now.

I think I was going about it wrong initially. I thought I had to make a class for the text files and their fields.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1