Import flat file: CSV, etc.

  • Thread starter Thread starter Adrian Miller
  • Start date Start date
A

Adrian Miller

Hi,

what's the best way to import a flat file to a .NET 3.5 App?

Thanks for any thought

Adrian
 
¤ Hi,
¤
¤ what's the best way to import a flat file to a .NET 3.5 App?
¤
¤ Thanks for any thought

Assuming some kind of column delimiter, one method would be to use the TextFieldParser:

Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Documents and
Settings\nfisppc\My Documents\My Database\Text\Orders.txt")

TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(",")

Dim CurrentRow As String()
While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
Dim CurrentField As String
For Each CurrentField In CurrentRow
Console.Write(CurrentField & Space(1))
Next
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
Console.WriteLine()
End While


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Hi,

what's the best way to import a flat file to a .NET 3.5 App?

Thanks for any thought

Adrian

Adrian,

The best way depends on the data you're trying to import - the number
of rows & columns, what are the field delimiters (or fixed width), any
text qualifiers, any special processing requirements, etc. It also
depends on what you want to do with it after you're imported it.

Here are a few general approaches:

You can manually read and process each line/record in a file. There
are classes to make this easier:
http://www.codeproject.com/KB/database/GenericParser.aspx

.... and full-blown, open source products that can do a lot of the
work:
http://www.filehelpers.com/


You can also use oledb (this is an ado.net newsgroup after all) to do
this:
http://www.codeproject.com/KB/database/ReadTextFile.aspx
http://www.codeproject.com/KB/cs/UsingJetForImport.aspx

HTH
-Jay
 
Back
Top