Solved

Reading Tab Delimited Data From text file using ODBC

Posted on 2006-10-24
4
963 Views
Last Modified: 2012-05-05
I found this code which extracts data from a text file -- Well and Good!   But the file is tab delimited and the routine seems to assume comma delimited values.
two questions:
1. Possible to instruct the ODBC driver to delimit by tab?
2. Possible to instruct the ODBC driver to use the first row as column headings (field names)?
Thank you.

            Dim path As String = "C:\ExtractTest\"
            Dim conn As New OdbcConnection( _
             "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & _
             path & ";")

            Dim cmd As New OdbcCommand("SELECT * FROM file22269.txt", conn)

            conn.Open()

            Dim dr As OdbcDataReader = _
             cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            While dr.Read()
                Dim test1 As String = dr.GetString(0)
            End While
            dr.Close()
            cmd.Dispose()
            conn.Dispose()
0
Comment
Question by:RichardKline
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Naveen Swamy earned 500 total points
ID: 17795112
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17795114
Dim arrColumnNames() As String
        Dim arrColumnValues() As String
        Dim arrRows() As String
        Dim i, j As Integer
        Dim dt As New System.Data.DataTable
        Dim row As DataRow
Dim path As String = "C:\ExtractTest\"
            Dim conn As New OdbcConnection( _
             "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & _
             path & ";")

            Dim cmd As New OdbcCommand("SELECT * FROM file22269.txt", conn)

            conn.Open()

            Dim dr As OdbcDataReader = _
             cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            While dr.Read()
                Dim test1 As String = dr.GetString(0)
            End While

arrRows = Split(test1, vbCrLf)
                arrColumnNames = Split(arrRows(0), delim)
                ''add columns to a datatable
                For n = 0 To arrColumnNames.Length - 1
                    dt.Columns.Add(New DataColumn(arrColumnNames(n), System.Type.GetType("System.String")))
                Next
                For i = 1 To arrRows.Length - 1
                    arrColumnValues = Split(arrRows(i), delim)
                    row = dt.NewRow
                    For j = 0 To (arrColumnNames.Length - 1)
                        Try
                            If Not IsDBNull(arrColumnValues(j)) Then
                                row(arrColumnNames(j)) = arrColumnValues(j)
                            Else
                                row(arrColumnNames(j)) = ""
                            End If
                        Catch ex As Exception
                            Console.Write("ERROR: " & ex.Message)
                        End Try
                    Next
                    dt.Rows.Add(row)
                Next
            dr.Close()
            cmd.Dispose()
            conn.Dispose()
0
 
LVL 1

Author Comment

by:RichardKline
ID: 17796394
I came up with this.  It allows me to use the header information in a datareader.   The method does require a schemi.ini file to be placed within the same folder as the tab delimited file.   Pretty flexible and simple to maintain.
Scema.ini contents:
[file22269.txt]
ColNameHeader=TRUE
Format=TabDelimited

The Code:
            Dim path As String = "C:\ExtractTest\"
            Dim strConnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                         path & ";Extended Properties=""text;HDR=YES;FMT=TabDelimited"";Persist Security Info=False"
            Dim conn As New OleDBConnection(strConnect)
            Dim cmd As New OleDbCommand("SELECT * FROM file22269.txt", conn)

            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            While dr.Read()
               ' do something ...
            End While
            dr.Close()
            cmd.Dispose()
            conn.Dispose()
0
 
LVL 1

Author Comment

by:RichardKline
ID: 17796446
navstar16, your links got me started into the right direction but there was a lot of digging....  

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now