importing text file into Sql Server table

lauriemc

Active member
Joined
Feb 2, 2007
Messages
25
I want to import a text file into a SQL Server table using a vb.net windows application. I have used the open file dialog box to select the text file, and have that file displayed in a text box, via a button click.

Has anybody done this ? I would sure like to see a code example,

thank you, lauriemc
 
Can you give us a clue as to what else you know/need? Do you know about ADO.NET, how to make a connection to a database? Do you know how to read a text file to parse it - presumably one line at a time? Is this just for fun, or a production application that needs lots of error handling?

-ner
 
I know more than I thought I did, I just had to think on it over the weekend. I did figure out that I could read the text file - which Im doing successfully, my problem now is parsing it. (Yes, I know how to make the SQL/database connection.)

When I have parsed before, Ive used an instr function, looking for a comma, because the comma has always been my field separator. However, now what I think I am looking at is a tab. (I dont know for sure, I had to open the text file in Word and then turn on all the unseen characters. It looks like a right-pointing arrow.) So I am stuck again, I dont know how to parse that string since it is using tabs.

Yes, its production, but I have done error handling before, I think I can figure it out. I apologize if my previous post did not give enough information, I hope this one does.

thank you lauriemc:(
 
String.Split

Its difficult to give specific advice without knowing the exact format of the text file, but from what youve described it sounds like each line of the file contains a single row of data to be inserted, with fields (columns) separated by tabs. You could therefore use the Split method of a String to split a line into an array using tab as a delimiter:

Code:
Dim values() As String
values = line.Split(ControlChars.Tab)

Now, values(0) will contain the first column value, values(1) the second, and so on.

If you can give more information about the format of the text file, perhaps by posting a small portion of it, then it will be easier to give helpful advice.

Good luck :)
 
Back
Top