0
我想通過vb.net將Excel文件導入Access數據庫 這個想法是客戶可以將數據導出到excel,修改它,添加,刪除它們導入回來了。使用vb.net將Excel文件上傳到MS Access中
導出的數據與導入的表格格式完全相同。 我使用下面的代碼:
Try
Dim strFileName As String = String.Empty
Dim XLda As New OleDbDataAdapter
Dim ExcelTables As New DataTable
Dim StrSelect = "SELECT * FROM [{0}]"
OpenFileDialog1.FileName = ""
OpenFileDialog1.InitialDirectory = mdlGlobalStuff.sMasterDataPath
OpenFileDialog1.Filter = "Excel|*.xls|All files (*.*)|*.*"
If OpenFileDialog1.ShowDialog() <> Windows.Forms.DialogResult.OK Then
Exit Sub
End If
strFileName = OpenFileDialog1.FileName
Dim MyXLConnection As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strFileName & ";Extended Properties=Excel 8.0;")
Using MyXLConnection
Using cmd As New OleDbCommand
cmd.Connection = MyXLConnection
cmd.CommandText = "INSERT INTO [MS Access;Database=InvoicingToolDB.accdb].[tbl_Bases] SELECT * FROM [Sheet1$]"
If MyXLConnection.State = ConnectionState.Open Then
MyXLConnection.Close()
End If
MyXLConnection.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As Exception
MsgBox("ImportLinkLabel_LinkClicked: Importing Base data" & vbCrLf & ErrorToString())
End Try
我一直有一個錯誤信息說:
Unrecognized database format 'c:\--path to db--\InvoicingToolDB.accdb'
的路徑是正確的,我不明白爲什麼格式將不被認可。
dunno如果它是你的一個選項,但我已經看到類似的做法是通過在Access數據庫VBA中導入一個函數,然後從VB.NET中調用該函數 –
你不能使用兩個不同的[數據庫源]在一個SQL行..你需要先讀取數據,然後打開連接到目標數據庫並插入那裏! –