Apparently I’m missing something while trying to use a schema.ini to define a csv. My code seems to ignore the schema.ini.
The schema.ini is placed in the same subdir as the csv and is as follows:
[excel.csv]
Format = CSVDelimited
Col1=TSP text width 17
Col2=Svc text width 17
Col3=DTM text width 3
The csv data which consists of a ‘heading’ row and a ‘data’ row is as follows:
"TSP","Svc","DTM"
"006958581","006927792","rdt"
The code I’m attempting to use is as follows:
Imports System.Data.OleDb
Module Module1
Sub Main()
Dim Csv_in_name As String = "excel.csv"
Dim Csv_in_path As String =
"C:\Documents and Settings\Administrator\My Documents" + _
"\Visual Studio 2008\Projects\csv_reader\csv_reader\bin\Debug"
Dim cn As New OleDbConnection
Dim adapter As New OleDbDataAdapter
Dim cmd As New OleDbCommand
Dim dtset As New DataSet
Dim dt As New DataTable
Dim cnstr As String =
"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data source = " + _
Csv_in_path + "\; Extended Properties=""Text;HDR=No;FMT=Delimited"""
cn.ConnectionString = cnstr
cn.Open()
cmd.Connection = cn
cmd.CommandText = "Select * from " + Csv_in_name
adapter.SelectCommand = cmd
adapter.Fill(dtset, "MyTable")
dt = dtset.Tables("MyTable")
cn.Close()
dt.WriteXml(Csv_in_path + "\data.xml")
End Sub
End Module
When HDR=No in the connection string, the code seems to ignore schema.ini as evidenced by the field delimiter names. Both rows are interpreted as data and the XML data is as follows:
<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
<MyTable>
<F1>TSP</F1>
<F2>Svc</F2>
<F3>DTM</F3>
</MyTable>
<MyTable>
<F1>006958581</F1>
<F2>006927792</F2>
<F3>rdt</F3>
</MyTable>
</NewDataSet>
When HDR=Yes, schema.ini is ignored as expected and the fields are properly labeled via the first row in the csv. The XML data is as follows:
<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
<MyTable>
<TSP>006958581</TSP>
<Svc>006927792</Svc>
<DTM>rdt</DTM>
</MyTable>
</NewDataSet>
Since the schema.ini is present in both instances, I would have expected 2 rows of data in the first instance but with the same field delimiters as in the second instance since these are the definitions in schema.ini. Why is schema.ini being ignored?
Change it to
HDR=No.HDRstands for Column Header.