vb.net:导入制表符分隔的文本文件以访问数据库

时间:2018-09-28 12:38:40

标签: vb.net tab-delimited-text

有人可以告诉我为什么我的代码为什么不导入制表符分隔的文本文件,但适用于逗号分隔的文本文件,我如何使它适用于制表符限定的文件?

代码如下

    Private Sub btnSelectFile_Click(sender As Object, e As EventArgs) Handles btnSelectFile.Click

    Dim AccessConn As New OleDb.OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source= (Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\Users\Me\Documents\ShipmentDB.accdb;")

    Dim FileName As String
    Dim FilePath As String
    Dim TargetPath As String
    Dim FilePathLength As Integer = 0

    'Get Text File Location that user need to import (AskForFile is a OpenDialogBox)

    AskForFile.Reset()
    AskForFile.Title = "Select File you want To inport"
    AskForFile.Filter = "Text File|*.txt|All Files|*.*"
    AskForFile.Multiselect = False
    AskForFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    AskForFile.ShowDialog()

    FileName = AskForFile.SafeFileName.ToString
    TargetPath = AskForFile.FileName.ToString
    FilePathLength = Len(TargetPath) - Len(FileName)

    FilePath = TargetPath.Substring(0, FilePathLength)

    Try
        AccessConn.Open()

        Dim DeleteTable As New System.Data.OleDb.OleDbCommand("DROP TABLE AmazonData;", AccessConn)
        DeleteTable.ExecuteNonQuery()

        Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT *  INTO AmazonData FROM [Text;DATABASE=" & FilePath & "].[" & FileName & "]", AccessConn)
        AccessCommand.ExecuteNonQuery()

    Catch ex As Exception
        AccessConn.Close()
        MessageBox.Show("Error Occured")
        Exit Sub
    End Try

    MessageBox.Show("Process Complete. Check and verify the database")

    AccessConn.Close()

End Sub

1 个答案:

答案 0 :(得分:0)

您应注意,您正在删除表,而不是删除数据。 在第一个命令中尝试“从AmazonData删除”,而不是“ DROP TABLE AmazonData”。