Private Sub import()
Try
'Dim Access As String = "C:\Users\Galaxy's_BR\Documents\Visual Studio 2013\Projects\try1\try1\bin\Debug\Data.accdb" *full path
Dim Access As String = "|DataDirectory|\Data.accdb" '*directory path
OpenFileDialog1.Filter = "Excel files (*.xls;*.xlsx;*.csv)|*.xls;*.xlsx;*.csv|All files (*.*)|*.*"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Excel = OpenFileDialog1.FileName
Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Excel & ";Extended Properties=""Excel 12.0 Xml;HRD=NO""")
Using cmd As OleDbCommand = New OleDbCommand("INSERT INTO [MS Access;Database=" & Access & ";PWD=xxxx].[SeatingTab] SELECT * FROM [Sheet1$]", conn)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
在上面的代码中, 当我使用访问数据库文件的完整路径字符串时,它可以按预期工作。但是,当我将其更改为目录路径时,显示错误“无效的文件名”,请指导我如何最小化此错误。 谢谢。