【问题标题】:How to merge two DataTables in a DataSet in Jet SQL?如何在 Jet SQL 的数据集中合并两个数据表?
【发布时间】:2014-01-07 01:16:37
【问题描述】:

另一个问题。对这些 SQL 的东西有点吃力。

我从 ADO.NET 中的查询生成 2 个表。我想从这两个表中执行第三个查询——它们不存在于连接的数据库中,但存在于数据集中。我无法为我的生活弄清楚如何做到这一点。我不想连接到数据库而是连接到数据集

我已经尝试过 DataReaders、TableAdapters、DataTable.Select(仅适用于一个表)和其他东西。答案是否在 LINQ for Datasets 中??

谢谢 安迪

    Dim strSelect As String
    Dim dsmcmd As OleDbDataAdapter          
    Dim dsm as New DataSet

    strSelect = "TRANSFORM Sum(Items.amount) AS total SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE (((Year([idate]))=2013) AND ((Items.category)<>3 Or (Items.category) Is Null) AND ((Accounts.accCategory)=6 OR (Accounts.accCategory)=7) AND ((Accounts.curr)=1)) GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment PIVOT Format(idate,'mmm') IN ('Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')"
    dsmcmd = New OleDbDataAdapter(strSelect, cn)
    dsmcmd.Fill(dsm, "Spent")

    strSelect = "TRANSFORM Sum(Items.amount) AS total SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE (((Year([idate]))=2013) AND ((Items.category)=3) AND ((Accounts.accCategory)=6) AND ((Accounts.curr)=1)) GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment PIVOT Format(idate,'mmm') IN ('Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')"
    dsmcmd = New OleDbDataAdapter(strSelect, cn)
    dsmcmd.Fill(dsm, "Allocated")        

    strSelect = "SELECT Totals.accCategory, Totals.ID, Totals.Account, Sum(Totals.Jan) AS Jan FROM (SELECT * FROM Allocated UNION SELECT * FROM Spent) AS Totals GROUP BY Totals.accCategory, Totals.ID, Totals.Account"
    dsmcmd = New OleDbDataAdapter(strSelect, <** WHAT DO I PUT HERE **>)
    dsmcmd.Fill(dsm, "Balance")        

【问题讨论】:

    标签: sql datatable dataset jet


    【解决方案1】:

    答案似乎有两个部分。

    如果数据表是在“内存中”生成的,则必须使用 LINQ

    Dim t = (From totals In (allocated.AsEnumerable.Union(spent.AsEnumerable)) _
                Group totals By accCategory = totals.Item("accCategory"), ID = totals.Item("ID"), Account = totals.Item("Account") _
                Into g = Group _
                Select New With {Key .accCategory = accCategory, Key .ID = ID, Key .Account = Account, Key .Jan = g.Sum(Function(totals) If(IsDBNull(totals.Item("Jan")), 0, CDec(totals.Item("Jan"))))}).ToList
    

    然后您必须将其转换回表格(例如,如果您想将其绑定到 DataGrid)

    Public Function ToTable(Of T)(data As IList(Of T)) As DataTable
    
        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
        Dim table As New DataTable()
    
        For Each prop As PropertyDescriptor In properties
            table.Columns.Add(prop.Name, If(Nullable.GetUnderlyingType(prop.PropertyType), prop.PropertyType))
        Next
    
        For Each item As T In data
            Dim row As DataRow = table.NewRow()
            For Each prop As PropertyDescriptor In properties
                row(prop.Name) = If(prop.GetValue(item), DBNull.Value)
            Next
            table.Rows.Add(row)
        Next
    
        Return table
    
    End Function
    

    唯一未解决的问题是 Null (DBNull) 处理 - 但我将发布一个单独的问题。

    安迪

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多