Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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 want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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 I'm doing successfully, my problem now is parsing it. (Yes, I know how to make the SQL/database connection.)

 

When I have parsed before, I've 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 don't 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 don't know how to parse that string since it is using tabs.

 

Yes, it's 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:(

Posted

String.Split

 

It's difficult to give specific advice without knowing the exact format of the text file, but from what you've 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:

 

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 :)

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...