Solved

Reading Tab Delimited Data From text file using ODBC

Posted on 2006-10-24
4
951 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
Comment Utility
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
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
Comment Utility
navstar16, your links got me started into the right direction but there was a lot of digging....  

Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

16 Experts available now in Live!

Get 1:1 Help Now