簡體   English   中英

導入多個文本文件以訪問數據庫

[英]Import multiple text file to access database

如何將多個文本文件導入數據庫表並根據日期和時間更新數據庫而不重復記錄的問題: -

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Student.mdb;Persist Security Info=True")
    Dim cmd As New System.Data.OleDb.OleDbCommand("INSERT INTO [Class] SELECT * FROM [Text;DATABASE=C:\Users\Documents\Visual Studio 2010\Projects\WebApplication10\WebApplication10\App_Data;].[Class.txt]")

    con.Open()
    With cmd
        .Connection = con
        .CommandType = CommandType.Text
        .ExecuteNonQuery()
        MsgBox("File Imported Successfully")
    End With
    con.Close()
End Sub

結束班

在Access數據庫表類中,確保包含日期和時間字段的字段是主鍵。 這樣可以防止將重復的條目導入表Class。

您可以輕松地將所有文本文件導入單獨的Access表,或將所有文本文件導入到一個Access表中(假設您感興趣的文件夾中的每個文件的架構都相同)。

Option Compare Database
Option Explicit
Private Sub Command0_Click()
    Call DoImport
End Sub

Function DoImport()

 Dim strPathFile As String
 Dim strFile As String
 Dim strPath As String
 Dim strTable As String
 Dim blnHasFieldNames As Boolean

 ' Change this next line to True if the first row in CSV worksheet
 ' has field names
 blnHasFieldNames = True

 ' Replace C:\Documents\ with the real path to the folder that
 ' contains the CSV files
 strPath = "C:\your_path_here\"

 ' Replace tablename with the real name of the table into which
 ' the data are to be imported

 strFile = Dir(strPath & "*.txt")

 Do While Len(strFile) > 0
       strTable = Left(strFile, Len(strFile) - 4)
       strPathFile = strPath & strFile

       'DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
       DoCmd.TransferText acLinkDelim, , strTable, strPathFile, blnHasFieldNames

 ' Uncomment out the next code step if you want to delete the
 ' EXCEL file after it's been imported
 '       Kill strPathFile

       strFile = Dir()
 Loop

End Function

Private Sub Command1_Click()

Dim strPathFile As String, strFile As String, strPath As String
        Dim strTable As String, strBrowseMsg As String
        Dim blnHasFieldNames As Boolean

        ' Change this next line to True if the first row in EXCEL worksheet
        ' has field names
        blnHasFieldNames = False

        strBrowseMsg = "Select the folder that contains the CSV files:"

        strPath = "C:\your_path_here\"

        If strPath = "" Then
              MsgBox "No folder was selected.", vbOK, "No Selection"
              Exit Sub
        End If

        ' Replace tablename with the real name of the table into which
        ' the data are to be imported
        strTable = "tablename"

        strFile = Dir(strPath & "\*.txt")
        Do While Len(strFile) > 0
              strPathFile = strPath & "\" & strFile

        DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames

        ' Uncomment out the next code step if you want to delete the
        ' EXCEL file after it's been imported
        '       Kill strPathFile

              strFile = Dir()
        Loop
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM