因此,我正在尝试在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和Excel2016。对于编程,我使用Visual Studio 2017中的VB.NET Framework 4.6.1。
答案 0 :(得分:0)
好吧,经过一些令人沮丧的时间,我设法弄清了一切。首先,我实现了@jmcilhinney提出的建议。然后我意识到我缺少键OleDBCommandBuilder
的功能,因此我添加了它。最后,我意识到合并功能实际上是完全不必要的,因此我摆脱了合并功能,而是将Excel数据直接导入到{ {1}}。在此之后,出现了一些麻烦,列名没有排列,但是在解决这些问题后,它起作用了!
当然,此代码的效率非常低而且凌乱,因此在清理后,最终代码如下:
DBDataSet
如您所见,我将其放在try-catch块中,将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
完全删除,并将ExcelDataSet
重命名为DBDataSet
,添加了@jmcilhinney的建议,还添加了一些注释。希望这可以帮助其他有此问题的人。