所以,我试图在 VB.NET 中创建一个程序,它将整个 Excel 文件导入到预先创建的 Access 中的新表中。我通过创建两个数据集来做到这一点:一个用于 Excel 文件中的所有数据,另一个用于数据库表(最初为空)。导入excel数据后,我然后合并两个数据集。因为第二个是空的,这实际上是在复制第一个。
到目前为止,这一切都是成功的。但是,在完成所有这些操作并调用 Update 命令之后,数据库没有发生任何变化。我已经检查过,数据集都是原样,但更改不会复制到数据库中。这是代码:
Sub ImportSheet2(ByVal ImportFileName As String, ByVal Month As String)
Dim DBConnection As OleDb.OleDbConnection
Dim ExcelConnection As OleDb.OleDbConnection
Dim DBAdapter As OleDb.OleDbDataAdapter
Dim ExcelAdapter As OleDb.OleDbDataAdapter
Dim DBDataSet As DataSet
Dim ExcelDataSet As DataSet
Dim DatabaseFilePath As String = "C:\Users\alexa\Documents\Alexander's folder\Visual Studio\Computer Science Coursework\Computer Science Coursework\Computer Science Coursework Database.accdb"
DBConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DatabaseFilePath)
ExcelConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ImportFileName + ";Extended Properties='Excel 12.0';")
DBConnection.Open()
ExcelConnection.Open()
ExcelAdapter = New OleDb.OleDbDataAdapter("select * from [Sheet1$]", ExcelConnection)
DBAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM January2018", DBConnection)
DBDataSet = New DataSet
ExcelDataSet = New DataSet
ExcelAdapter.Fill(ExcelDataSet)
DBDataSet.Merge(ExcelDataSet, preserveChanges:=False)
Builder = New OleDb.OleDbCommandBuilder(DBAdapter)
DBAdapter.UpdateCommand = Builder.GetUpdateCommand()
DBAdapter.Update(DBDataSet)
DBConnection.Dispose()
ExcelConnection.Dispose()
End Sub
没有发生错误,所以我不确定这里的问题是什么。我的 Access 数据库也有多个表,我不知道如何让它只更新我指定的一个。
对于软件,我使用的是 Office 2016、Access 2016 和 Excel 2016。对于编程,我使用的是 Visual Studio 2017 中的 VB.NET Framework 4.6.1。
好吧,经过一段非常令人沮丧的时间后,我设法弄清楚了这一切。首先,我实施了@jmcilhinney 给出的建议。然后我意识到我缺少一个关键OleDBCommandBuilder
功能,所以我添加了它。最后,我意识到合并功能实际上是完全没有必要的,所以我摆脱了它,而是将 Excel 数据直接导入到DBDataSet
. 在此之后,列名没有对齐出现了一些并发症,但是在解决了这些问题之后,它起作用了!
当然,这段代码效率低下而且乱七八糟,所以清理干净后的最终代码是这样的:
Sub ImportSheet2(ByVal ImportFileName As String, ByVal Month As String)
Dim DBConnection As OleDb.OleDbConnection
Dim ExcelConnection As OleDb.OleDbConnection
Dim DBAdapter As OleDb.OleDbDataAdapter
Dim ExcelAdapter As OleDb.OleDbDataAdapter
Dim TransferDataSet As DataSet
Dim Builder As OleDb.OleDbCommandBuilder
Dim DatabaseFilePath As String = "C:\Users\alexa\Documents\Alexander's folder\Visual Studio\Computer Science Coursework\Computer Science Coursework\Computer Science Coursework Database.accdb"
DBConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DatabaseFilePath) 'These open the two connections to the database and excel file.
ExcelConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ImportFileName + ";Extended Properties='Excel 12.0';")
DBConnection.Open()
ExcelConnection.Open()
Try
ExcelAdapter = New OleDb.OleDbDataAdapter("select * from [Sheet1$]", ExcelConnection) 'These create the adapters for the dataset.
DBAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM January2018", DBConnection)
ExcelAdapter.AcceptChangesDuringFill = False 'This ensures that the changes made will be transferred to the database.
TransferDataSet = New DataSet 'This makes the dataset that hold the data from the Excel file, ready to be imported to the database.
ExcelAdapter.Fill(TransferDataSet) 'This imports all the excel data into the dataset, ready to be transferred to the database.
Builder = New OleDb.OleDbCommandBuilder(DBAdapter) 'These builder functions execute the SQL commands necessary to update the database.
Builder.GetUpdateCommand()
DBAdapter.UpdateCommand = Builder.GetUpdateCommand()
DBAdapter.Update(TransferDataSet) 'Finally, the dataset is updated with the changes, which are carried over to the database.
Catch ex As Exception
MessageBox.Show(ErrorToString)
End Try
DBConnection.Dispose()
ExcelConnection.Dispose()
End Sub
正如你所看到的,我把它放在一个 try-catch 块中,ExcelDataSet
完全删除了并重命名DBDataSet
为TransferDataSet
,添加了@jmcilhinney 的建议,并添加了一些评论。希望这可以帮助其他遇到此问题的人。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句