3

我正在使用 Microsoft Access 数据库(请不要建议我使用其他数据库,因为在这种特殊情况下我没有那个选择)。由于 Access 数据库表不能超过 255 列,因此我使用垂直数据库分区技术,将信息拆分为 5 个不同的表。每个表有 100 列,第一列是主键。(请不要在这里建议我进行数据库规范化)。现在我需要从这五个表中读取数据,以便将其导出到 Excel 文件中。

为了实现这一点,我已经完成了以下步骤,但无法弄清楚如何组合多个表。

  1. 我正在阅读这些表中的每一个并将单独的 DataTable 放入同一个 DataSet 中。(由于我使用的是 Access 数据库,我无法从所有表中选择所有列)

  2. 我已经在这五个表上声明了关系

  3. 现在我需要将这些 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
    
4

2 回答 2

2

我不会打扰 DataRelations,只会使用Merge方法。然后像这样破解其余的...

    'Make all of the DataTables have the same columns.
    For Each dtPrepForMerge As DataTable In FinalDS.Tables

        For Each dtColumnsToGrab As DataTable In FinalDS.Tables

            For Each dc As DataColumn In dtColumnsToGrab.Columns

                'Don't duplicate columns.
                If dtPrepForMerge.Columns(dc.ColumnName) Is Nothing Then

                    dtPrepForMerge.Columns.Add(New DataColumn(dc.ColumnName, dc.DataType))

                End If

            Next

        Next

    Next

    'Now you can merge all of the tables together.
    'The order of the columns don't have to be the same between the tables.
    For intTableMergeCursor As Integer = 1 To (FinalDS.Tables.Count - 1)

        FinalDS.Tables(0).Merge(FinalDS.Tables(intTableMergeCursor))

    Next

    'Clone a new table that will hold your final results.
    Dim dtFinalResult As DataTable = FinalDS.Tables(0).Clone()

    'Sort the rows of your merged table by ID.
    Dim arrSortedDataRows As DataRow() = FinalDS.Tables(0).Select("id > 0", "id")

    Dim intLastID As Integer = CInt(arrSortedDataRows(0).Item("id"))

    Dim drMergedRow As DataRow = dtFinalResult.NewRow()

    For Each dr As DataRow In arrSortedDataRows

        'New ID?
        If CInt(dr.Item("id")) <> intLastID Then

            intLastID = CInt(dr.Item("id"))

            dtFinalResult.Rows.Add(drMergedRow)

            drMergedRow = dtFinalResult.NewRow()

        End If

        For intColumnCursor As Integer = 0 To (dtFinalResult.Columns.Count - 1)

            'Don't overwrite values.
            If Not dr.Item(intColumnCursor) Is DBNull.Value Then

                drMergedRow.Item(intColumnCursor) = dr.Item(intColumnCursor)

            End If

        Next

    Next

    dtFinalResult.Rows.Add(drMergedRow)
于 2012-04-06T21:03:05.030 回答
0

使用 datatable.columns.add - 请参阅http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx#Y166

下面是未经测试的代码,可能无法编译,但想法是遍历所有数据表并将列添加到一个大数据表中。

Dim HugeDT as new datatable

For dt as datatable in FinalDS.DataTables
    For col as datacolumn in dt.Columns
        HugeDT.columns.add (col)
    Next
    'Run a loop on rows of each datatable to copy over the rows.
    'Remember to correlate the recordpointer correctly for each iteration
Next
于 2012-04-06T19:11:58.273 回答