将Excel文件上传到MS Access中,可以通过以下步骤实现:
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim excelFilePath As String = "C:\path\to\excel\file.xlsx"
Dim accessFilePath As String = "C:\path\to\access\database.accdb"
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & accessFilePath & ";Persist Security Info=False;"
Dim excelConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & excelFilePath & ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;'")
Try
excelConnection.Open()
Dim excelCommand As New OleDbCommand("SELECT * FROM [Sheet1$]", excelConnection)
Dim excelReader As OleDbDataReader = excelCommand.ExecuteReader()
Dim accessConnection As New OleDbConnection(connectionString)
accessConnection.Open()
Dim accessCommand As New OleDbCommand("INSERT INTO TableName (Field1, Field2, Field3) VALUES (@Value1, @Value2, @Value3)", accessConnection)
accessCommand.Parameters.AddWithValue("@Value1", "")
accessCommand.Parameters.AddWithValue("@Value2", "")
accessCommand.Parameters.AddWithValue("@Value3", "")
While excelReader.Read()
accessCommand.Parameters("@Value1").Value = excelReader(0).ToString()
accessCommand.Parameters("@Value2").Value = excelReader(1).ToString()
accessCommand.Parameters("@Value3").Value = excelReader(2).ToString()
accessCommand.ExecuteNonQuery()
End While
MessageBox.Show("Excel data uploaded to MS Access successfully.")
excelReader.Close()
excelConnection.Close()
accessConnection.Close()
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
End Class
请注意,上述代码中的"TableName"应替换为目标数据库中的表名,"Field1"、"Field2"、"Field3"应替换为目标表中的字段名。
这是一个基本的示例,你可以根据实际需求进行修改和扩展。如果需要更多关于ADO.NET和VB.NET的信息,可以参考腾讯云的相关文档和教程:
希望以上信息对你有帮助!
领取专属 10元无门槛券
手把手带您无忧上云