Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Importing Delimited Text Files
Message
From
27/12/2004 12:53:15
 
 
To
20/12/2004 22:17:30
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00970872
Message ID:
00972389
Views:
10
This message has been marked as the solution to the initial question of the thread.
>Just would like to know how (if it is possible) to import a text file
>delimited with a pipe or vertical bar (|) into a DataTable.

You could use the StreamReader class to read each line of the text file. Then, use String.Split to populate a string array with the pipe-delimited values. Finally, add a new row to the DataTable and populate it with the contents of the array (converting the data type where necessary).

The code (VB.NET) could look something like this:
' Assume dt is DataTable reference

Dim RowToAdd As DataRow
Dim InStr As String
Dim InData() As String
Dim InFile As StreamReader
InFile = File.OpenText("c:\example\example.txt")

While InFile.Peek > -1
   InStr = InFile.ReadLine
   InData = Split(InStr, "|")
   RowToAdd = dt.NewRow()
   RowToAdd(0) = CType(InData(0),Integer)
   RowToAdd(1) = Indata(1)
   ' etc. Or, use LoadDataRow.
   dt.Rows.Add(RowToAdd)
End While
See ADO.NET and You, May 22, 2002, for a similar (but different) example (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet05282002.asp).
Christopher Bohling, Consultant
http://www.ChristopherBohling.com
Previous
Reply
Map
View

Click here to load this message in the networking platform