3

我用这个代码导入一个文本文件(.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 )
4

1 回答 1

0

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

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

于 2013-04-18T08:32:20.337 回答