Vb6 如何使用VB读取CSV文件并将数据插入Access数据库

Vb6 如何使用VB读取CSV文件并将数据插入Access数据库,vb6,Vb6,我是新的VB编程,我有一个要求,其中,将有一个表,这是已经在Access中创建,我需要编写代码,将读取文件,并在记录中输入数据。请帮我解决这个问题。我使用Access作为我的数据库 输入文件为.CSV文件,其格式为: FIRST_NAME, LAST_NAME, HOME_ADD, COMPANY, DESIG Mark , Taylor , UK , XYZ , ABC Mark , Taylor , UK , XYZ , ABC Mark , Taylor , UK , XYZ , ABC

我是新的VB编程,我有一个要求,其中,将有一个表,这是已经在Access中创建,我需要编写代码,将读取文件,并在记录中输入数据。请帮我解决这个问题。我使用Access作为我的数据库

输入文件为.CSV文件,其格式为:

FIRST_NAME, LAST_NAME, HOME_ADD, COMPANY, DESIG
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
Mark , Taylor , UK , XYZ , ABC
此值不能从已创建的Access表中的“C:\input\data.csv”文件夹读写


提前感谢

我不确定您是否会提前知道字段。我假设在文件打开之前您不知道。另外,我假设您正在使用ADO

Private Sub InsertCsvDataIntoTable(ByRef in_sCsvFileName As String, _
                                   ByRef in_oConn As ADODB.Connection, _
                                   ByRef in_sTableName As String)

    Dim iFileNo             As Integer
    Dim sLine               As String
    Dim vasFields           As Variant
    Dim lIndex              As Long
    Dim sSQL_InsertPrefix   As String
    Dim sSQL                As String

    iFileNo = FreeFile

    Open in_sCsvFileName For Input As #iFileNo

    If EOF(iFileNo) Then
        Exit Sub
    End If

    ' Read field names from the top line.
    Line Input #iFileNo, sLine

    ' Note that in a "proper" CSV file, there should not be any trailing spaces, and all strings should be surrounded by double quotes.
    ' However, the top row provided can simply be used "as is" in the SQL string.

    sSQL_InsertPrefix = "INSERT INTO " & in_sTableName & "(" & sLine & ") VALUES("

    Do Until EOF(iFileNo)

        Line Input #iFileNo, sLine

        ' Initialise SQL string.
        sSQL = sSQL_InsertPrefix

        ' Again, in a proper CSV file, the spaces around the commas shouldn't be there, and the fields should be double quoted.
        ' Since the data is supposed to look like this, then the assumption is that the delimiter is space-comma-space.
        vasFields = Split(sLine, " , ")

        ' Build up each value, separated by comma.
        ' It is assumed that all values here are string, so they will be double quoted.
        ' However, if there are non-string values, you will have to ensure they don't get quoted.
        For lIndex = 0 To UBound(vasFields) - 1
            sSQL = sSQL & "'"
            sSQL = sSQL & vasFields(lIndex)
            sSQL = sSQL & "'"
            sSQL = sSQL & ","
        Next lIndex

        ' This chunk of code is for the last item, which does not have a following comma.
        sSQL = sSQL & "'"
        sSQL = sSQL & vasFields(lIndex)
        sSQL = sSQL & "'"

        sSQL = sSQL & ")"

        ' Run the SQL command.
        in_oConn.Execute sSQL

    Loop

    Close #iFileNo

End Sub

Access已经支持从CSV文件导入记录,因此通过启动和控制Access实例可以非常轻松地完成这项工作

With CreateObject("Access.Application")
    .OpenCurrentDatabase "C:\Database1.accdb"
    .DoCmd.TransferText , , "Table1", "c:\input\data.csv", True
    .Quit
End With

TransferText
True
参数指定您的CSV文件包含标题行。

您是否尝试过任何操作?是的,但我所做的是读取一个文件并创建一个新表显示其中的数据。我不明白您的意思。但是,如果您已经编写了不起作用的代码,为什么不发布呢?hi@MarkBertenshaw我所做的是创建一个新表并将数据映射到该表,但是需要映射到已经创建的表,但这是正确的答案吗?您的CSV文件看起来有点奇怪(请参阅我的客户评论)-此示例数据是您自己创建的,还是真实的数据?