Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Can anyone give me a simple example in Visual Basic.NET of how to import a text file into a Microsoft Access Database, into an existing table. Any help with this would be greatly appreiciated.
Posted

Try This:

       Dim strContents As String
       Dim objReader As StreamReader
       Try
           objReader = New StreamReader("textfile.txt")
           strContents = objReader.ReadToEnd()
           objReader.Close()
           Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.mdb")
           Dim MyCommand As New OleDbCommand("INSERT INTO table (text) VALUES ('" & strContents & "')", MyConnection)
           MyConnection.Open()
           MyCommand.ExecuteNonQuery()
           MyConnection.Close()
           MyCommand.Dispose()
       Catch
       End Try

 

Make sure you have imported this:

Imports System.IO
Imports System.Data.OleDb

Posted

need a little more help

 

The example you provided to me worked great, however I have multiple rows and columns of in this text file. Each row is a new record and each column is a new field. When I run the program it reads the entire text file into one record in one field of the database. I do I get it to read multiple records into the database from the text file. Again any help given is greatly apprieciated.

Posted

place some text that can be find in the textfile.

 

to read the file line by line use this:

 

Dim file As String = "textfile.txt"
Dim strLine As String
Dim intNr As Integer
intNr = FreeFile()
FileOpen(intNr, file, OpenMode.Input)

Do
strLine = LineInput(intNr)
'add database input here, 
Loop Until EOF(intNr)
FileClose(intNr)

 

Greetz

Posted

Better (read: hell of a lot faster) to use IO.StreamReader class:

 

Dim sFile As String = "textfile.txt" 
Dim sLine As String 
Dim SR as New IO.StreamReader(file)
While SR.Peek <> -1
  sLine = SR.ReadLine
End While
SR.Close

Posted

Another quick question

 

I can read the line in by record now, thanks, but how do I get the program to seperate the text in to the proper fields, the text is seperated by commas. As always any help given is greatly appreciated.

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...