Solved

Reading Tab Delimited Data From text file using ODBC

Posted on 2006-10-24
4
998 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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