如何根据日期和时间将多个文本文件导入数据库表并更新数据库而不重复记录的问题:-
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
结束班级
答案 0 :(得分:0)
在Access数据库表Class中,确保包含日期和时间字段的字段是主键。这将防止将重复的条目导入表Class。
答案 1 :(得分:0)
您可以轻松地将所有文本文件导入到单独的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