比较2个数据集(一对多行)

时间:2017-10-19 23:30:13

标签: vb.net winforms dataset

VB.NET Winforms

我需要比较2个数据表(每个数据集都在它们自己的数据集中)并以OK或NG的判断结束,一个是参考表,另一个是来自机器的原始数据。一个表有一行带有QTY,另一个表有多行可以匹配该QTY,希望如此。这里的目的是验证紧固件上的扭矩值。这里的第一个表格是我将得到的数据,即扭矩传递所需的数据。我们称这个表为dtTorquesRequired

NINDEX      NQTY_REQ    NMIN    NMAX
7           1           33.0    59.0
8           4           33.0    59.0
9           2           4.5     7.5
12          2           4.5     7.5

NINDEX将是2个数据集之间的公共列。

我们以最后一行为例。最后一行告诉我,我需要从NINDEX 12获得2个扭矩,其值介于4.5 - 7.5之间。

这是我必须判断的数据。我们称这个表为dtTorquesPerformed

NINDEX              NTORQUE_NO NTORQUE_STATUS NTORQUE_VALUE
7                    1          1              42.56
8                    1          1              42.22
8                    2          1              42.49
8                    3          1              42.10
8                    4          1              42.37
9                    1          1              7.01
9                    2          1              7.10
12                   1          1              5.68
12                   2          1              5.81

判断需要在2个表之间匹配NINDEX,NTORQUE_STATUS必须为1,并且NTTQUE_VALEU必须在dtTorquesRequired中的NMIN和NMAX之间。

现在,我目前正在使用SQL行计数结果在我的应用程序中执行此操作,但速度很慢。我是通过多个聊天调用而不是对数据库进行粗略调用来完成的,然后使用生成的数据集。

这就是我目前的做法,但速度很慢。我想在本地进行所有处理而不是使用SQL:

Private Function TorqueJudgement(ckanban As String) As Boolean
    Dim ccode As String
    Dim cline As String
    Dim dsTorquesRequired As New DataSet
    Dim dtTorquesRequired As New DataTable
    Dim AreAllTorquesGood As Boolean = False
    Dim BadTorqueCount As Int16 = 0

    ccode = ckanban.Substring(0, 5)
    cline = ckanban.Substring(5, 2)

        SQL.ExecQuery("SELECT ....") ''GETS DTTORQUESREQUIRED

    dsTorquesRequired = SQL.SQLDS
    dtTorquesRequired = dsTorquesRequired.Tables(0)
    dtTorquesRequired.Columns.Add("PassFail")

    Dim rc As Int16 = -1 '// 
    For Each dr As DataRow In dsTorquesRequired.Tables(0).Rows
        rc += 1
        Dim nindex As Int16 = dr("NINDEX")
        Dim qtyrequired As Int16 = dr("NQTY_REQ")
        Dim nmin As Decimal = dr("NMIN")
        Dim nmax As Decimal = dr("NMAX")

        SQL.AddParam("@CKANBAN", ckanban)
        SQL.AddParam("@NINDEX", nindex)
        SQL.AddParam("@QTY_REQ", qtyrequired)
        SQL.AddParam("@NMIN", nmin)
        SQL.AddParam("@NMAX", nmax)
        SQL.ExecQuery("SELECT ...") ''Searches for rows that match dtTorquesRequired

        dtTorquesRequired.Rows(rc)("PassFail") = SQL.RecordCount
        If Not dtTorquesRequired.Rows(rc)("NQTY_REQ") <= dtTorquesRequired.Rows(rc)("PassFail") Then
            BadTorqueCount += 1
        End If
    Next

    If BadTorqueCount > 0 Then
        AreAllTorquesGood = False
    ElseIf BadTorqueCount = 0 Then
        AreAllTorquesGood = True
    End If

    Return AreAllTorquesGood
End Function

如何在不使用SQL

的情况下使用VB.NET在本地查询数据表对象

提前致谢

1 个答案:

答案 0 :(得分:1)

您可以使用DataView个对象“查询”DataTables

Dim dtb As New DataTable
dtb.Columns.Add("Col0")
dtb.Columns.Add("Col1")
dtb.Columns.Add("Col2")
dtb.Columns.Add("Col3")
dtb.Rows.Add("R0C0", "R0C1", "A", "A")
dtb.Rows.Add("R1C0", "R1C1", "A", "A")
dtb.Rows.Add("R2C0", "R2C1", "A", "B")
dtb.Rows.Add("R3C0", "R3C1", "B", "B")

Dim dvw As New DataView(dtb)
dvw.RowFilter = "Col0>'R1C0'" 'WHERE 
dvw.Sort = "Col2 DESC" 'ORDER BY
For Each drv As DataRowView In dvw
  Dim drw As DataRow = drv.Row
  Console.WriteLine(drw("Col0") & " " & drw("Col1") & " " & drw("Col2") & " " & drw("Col3"))
Next
Dim dtbDistinct As DataTable = dtb.DefaultView.ToTable(True, {"Col2", "Col3"}) 'DISTINCT
For Each drw As DataRow In dtbDistinct.Rows
  Console.WriteLine(drw("Col2") & " " & drw("Col3"))
Next
Console.ReadKey()