[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reading Tab Delimited Data From text file using ODBC

Posted on 2006-10-24
4
Medium Priority
?
1,024 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 1500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 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