我正在使用 Microsoft Access 数据库(请不要建议我使用其他数据库,因为在这种特殊情况下我没有那个选择)。由于 Access 数据库表不能超过 255 列,因此我使用垂直数据库分区技术,将信息拆分为 5 个不同的表。每个表有 100 列,第一列是主键。(请不要在这里建议我进行数据库规范化)。现在我需要从这五个表中读取数据,以便将其导出到 Excel 文件中。
为了实现这一点,我已经完成了以下步骤,但无法弄清楚如何组合多个表。
我正在阅读这些表中的每一个并将单独的 DataTable 放入同一个 DataSet 中。(由于我使用的是 Access 数据库,我无法从所有表中选择所有列)
我已经在这五个表上声明了关系
现在我需要将这些 DataTable 组合成一个 DataTable。这意味着我将总共有 496 列。我正在从 4 个表中删除主键,因为它是相同的信息。我该怎么做呢?
Dim conn As OleDbConnection = New OleDbConnection Conn ="Connection string stuff goes" Dim FinalDS As New DataSet Dim DataSet1 As New DataSet Dim DataSet2 As New DataSet SQLstr1 = "SELECT * FROM Table1" SQLstr2 = "SELECT * FROM Table2" SQLstr3 = "SELECT * FROM Table3" SQLstr4 = "SELECT * FROM Table4" SQLstr5 = "SELECT * FROM Table5" Dim DA1 As New OleDb.OleDbDataAdapter(SQLstr1, cn) Dim DA2 As New OleDb.OleDbDataAdapter(SQLstr2, cn) Dim DA3 As New OleDb.OleDbDataAdapter(SQLstr3, cn) Dim DA4 As New OleDb.OleDbDataAdapter(SQLstr4, cn) Dim DA5 As New OleDb.OleDbDataAdapter(SQLstr5, cn) Try DA1.Fill(FinalDS, "Tl") DA2.Fill(FinalDS, "T2") DA3.Fill(FinalDS, "T3") DA4.Fill(FinalDS, "T4") DA5.Fill(FinalDS, "T5") Dim DataRelation1 As New DataRelation("R1", _ FinalDS.Tables("T1").Columns("ID"), _ FinalDS.Tables("T2").Columns("ID")) Dim DataRelation2 As New DataRelation("R2", _ FinalDS.Tables("T2").Columns("ID"), _ FinalDS.Tables("T3").Columns("ID")) Dim DataRelation3 As New DataRelation("R3", _ FinalDS.Tables("T3").Columns("ID"), _ FinalDS.Tables("T4").Columns("ID")) Dim DataRelation4 As New DataRelation("R4", _ FinalDS.Tables("T4").Columns("ID"), _ FinalDS.Tables("T5").Columns("ID")) FinalDS.Relations.Add(DataRelation1) FinalDS.Relations.Add(DataRelation2) FinalDS.Relations.Add(DataRelation3) FinalDS.Relations.Add(DataRelation4) FinalDS.Relations.Add(DataRelation5) 'Here I need to combine all these five DataTables into one DataTable 'So that I can use Export it to excel from that one Final DataTable instead of reading data from five different DataTables. 'My Exporting to excel logies goes here which simply supporse to reads data from final DataTable which has 496 columns. Catch ex As Exception MsgBox(ex.Message) End Try