C
Chris
Hi
I can use a text file as a datasource but am unable to get the datatable to
see the text file as having multiple columns. Everything gets
put into the first column in the datatable. Sample of code and text file
included.
Please help
Regards
Chris
Public Function ImportTextFile(ByVal Path As String, ByVal File As String)
As String
Dim txtConStr As String
Dim txtoleCon As OleDb.OleDbConnection
Dim txtoleAdapter As OleDb.OleDbDataAdapter
Dim txtdataset As DataSet
Dim txtdatarow As DataRow
Try
Dim f As System.IO.File
If f.Exists(Path & "\" & File) Then
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text\FirstRowHasNames
=00 (00=False) (01=True)
txtConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Path & ";Extended Properties=Text;"
txtoleCon = New OleDb.OleDbConnection(txtConStr)
txtoleCon.Open()
txtoleAdapter = New OleDb.OleDbDataAdapter("Select * from "
& File, txtoleCon)
txtoleAdapter.SelectCommand.ExecuteNonQuery()
txtdataset = New DataSet
txtoleAdapter.Fill(txtdataset, "TextFile")
For Each txtdatarow In txtdataset.Tables(0).Rows
Me.lbRecords.Items.Add(txtdatarow.Item(0))
Next
txtoleCon.Close()
Return "True"
Else
Return "File Not Found"
End If
Catch ex As Exception
Return False
Finally
txtdataset = Nothing
txtoleAdapter = Nothing
txtoleCon = Nothing
End Try
End Function
'Text File Sample
11966 GAS_SMP 2003/11/27 17:06:00 000 0.0
11966 GAS_SMP 2003/11/27 17:07:00 000 0.0
11966 GAS_SMP 2003/11/27 17:08:00 000 0.0
11966 GAS_SMP 2003/11/27 17:09:00 000 0.0
11966 GAS_SMP 2003/11/27 17:10:00 000 0.0
11966 GAS_SMP 2003/11/27 17:11:00 000 0.0
11966 GAS_SMP 2003/11/27 17:12:00 000 0.0
11966 GAS_SMP 2003/11/27 17:13:00 000 0.0
11966 GAS_SMP 2003/11/27 17:14:00 000 0.0
11966 GAS_SMP 2003/11/27 17:15:00 000 0.0
11966 GAS_SMP 2003/11/27 17:16:00 000 0.0
11966 GAS_SMP 2003/11/27 17:17:00 000 0.0
I can use a text file as a datasource but am unable to get the datatable to
see the text file as having multiple columns. Everything gets
put into the first column in the datatable. Sample of code and text file
included.
Please help
Regards
Chris
Public Function ImportTextFile(ByVal Path As String, ByVal File As String)
As String
Dim txtConStr As String
Dim txtoleCon As OleDb.OleDbConnection
Dim txtoleAdapter As OleDb.OleDbDataAdapter
Dim txtdataset As DataSet
Dim txtdatarow As DataRow
Try
Dim f As System.IO.File
If f.Exists(Path & "\" & File) Then
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Text\FirstRowHasNames
=00 (00=False) (01=True)
txtConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& Path & ";Extended Properties=Text;"
txtoleCon = New OleDb.OleDbConnection(txtConStr)
txtoleCon.Open()
txtoleAdapter = New OleDb.OleDbDataAdapter("Select * from "
& File, txtoleCon)
txtoleAdapter.SelectCommand.ExecuteNonQuery()
txtdataset = New DataSet
txtoleAdapter.Fill(txtdataset, "TextFile")
For Each txtdatarow In txtdataset.Tables(0).Rows
Me.lbRecords.Items.Add(txtdatarow.Item(0))
Next
txtoleCon.Close()
Return "True"
Else
Return "File Not Found"
End If
Catch ex As Exception
Return False
Finally
txtdataset = Nothing
txtoleAdapter = Nothing
txtoleCon = Nothing
End Try
End Function
'Text File Sample
11966 GAS_SMP 2003/11/27 17:06:00 000 0.0
11966 GAS_SMP 2003/11/27 17:07:00 000 0.0
11966 GAS_SMP 2003/11/27 17:08:00 000 0.0
11966 GAS_SMP 2003/11/27 17:09:00 000 0.0
11966 GAS_SMP 2003/11/27 17:10:00 000 0.0
11966 GAS_SMP 2003/11/27 17:11:00 000 0.0
11966 GAS_SMP 2003/11/27 17:12:00 000 0.0
11966 GAS_SMP 2003/11/27 17:13:00 000 0.0
11966 GAS_SMP 2003/11/27 17:14:00 000 0.0
11966 GAS_SMP 2003/11/27 17:15:00 000 0.0
11966 GAS_SMP 2003/11/27 17:16:00 000 0.0
11966 GAS_SMP 2003/11/27 17:17:00 000 0.0