如何解析ASP.NET网站中的CSV文件?

Par*_*att 1 .net csv parsing

在我的网站上,我有许多CSV文件需要解析并将其数据插入我的MySQL数据库.

如何以编程方式在我的网站中解析CSV?

Nic*_*ale 6

我建议在.Net中查看TextFieldParserClass.你需要包括

Imports Microsoft.VisualBasic.FileIO.TextFieldParser
Run Code Online (Sandbox Code Playgroud)

这是一个快速示例:

Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(FileName)
Dim CurrentRecord As String() ' this array will hold each line of data
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True

' parse the actual file
Do While Not afile.EndOfData
    Try
        CurrentRecord = afile.ReadFields
    Catch ex As FileIO.MalformedLineException
        Stop
    End Try
Loop
Run Code Online (Sandbox Code Playgroud)