使用ADO.net(OLEDB)导入UTF-8 CSV文件

时间:2012-11-30 08:03:17

标签: .net vb.net csv utf-8

我使用此代码导入文本文件(.CSV),但是当此文件由UTF-8代码创建时,它无法成功。只有我保存为ANSI代码(使用Note.exe),它可以没问题。

Function getDataset(ByVal sql As String, ByVal path As String) As DataSet
    'Try
    'Dim connstr As String = GetINI("Setting", "cString",     "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\DataSouce\DataBase.mdb", Application.StartupPath & "\setting.ini")
    Dim connstr As String = "Provider=Microsoft.jet.OLEDB.4.0;Data Source= " & path & "\;Extended Properties=""text;HDR=YES;FMT=Delimited"""
    Dim conn As OleDbConnection = New OleDbConnection(connstr)                   '("provider=microsoft.Jet.OLEDB.4.0;data source=" & CurDir() & "\data.mdb")
    Dim adapter1 As OleDb.OleDbDataAdapter = New OleDbDataAdapter(sql, conn)
    Dim cmd As OleDbCommand = New OleDbCommand
    Dim commandBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter1)
    getDataset = New DataSet
    adapter1.Fill(getDataset, "temp")
    'Catch ex As Exception
    '    MsgBox(ex.Message)
    'End Try

 End Function

我甚至尝试构建这样的schema.ini,也不行

[temp.CSV]
Format=Delimited(,)
ColNameHeader=false
MaxScanRows=0
CharacterSet= ANSI  
 (or  UTF-8 )

1 个答案:

答案 0 :(得分:0)

VB有一个内置的csv解析器。看看这篇文章:http://coding.abel.nu/2012/06/built-in-net-csv-parser/

TextFieldParser位于Microsoft.VisualBasic.FileIO名称空间中。如果要在C#中构建模块,则必须导入它。