0

我有 2 个数据集。命名为ds,ds1。数据集值包含这样的值

dataset(values from excel sheet)
--------
no  phone       title
91  9942321400  MR
91  9865015695  MR
91  9677031515  MR
91  9994828285  MR
91  9688104229  MR


dataset1 contain value like this(values from mysql table)
-------------------------------
phone
9942321400
9865015695
9677031515

比较 2 个数据集。如果数据集不等于 datset1 phone ,我们必须在记事本中写入该数据集 phone no 。但结果错误。

mycode

Dim i As Integer = 0
        Do While (i <= ds1.Tables(0).Rows.Count - 1)
            Dim phone As String = ds1.Tables(0).Rows(i).Item(1).ToString
            Dim j As Integer = 0
            Do While (j <= Ds.Tables(0).Rows.Count - 1)
                Dim dumphone As String = Ds.Tables(0).Rows(j).Item(4).ToString
                If (dumphone <> phone) Then
                    TextBox1.AppendText(a.ToString & "|" & b.ToString & "|" & c.ToString)
                    sw.WriteLine(TextBox1.Text)
                    TextBox1.Text = ""
                End If
                j = (j + 1)
                'i = i + 1
            Loop
            i = (i + 1)
        Loop

我在记事本中的结果


|91|9942321400|MR
|91|9942321400|MR
|91|9942321400|MR
|91|9942321400|MR
|91|9865015695|MR
|91|9865015695|MR
|91|9865015695|MR
|91|9865015695|MR
|91|9677031515|MR
|91|9677031515|MR
|91|9677031515|MR
|91|9677031515|MR    


but expected output in notepad like this
----------------------------------------
91|9994828285|MR
91|9688104229|MR
4

2 回答 2

1

您的代码正在比较第二个数据集的每一行与第一行的每一行。

你的 Do While 块遍历整个事情,没有排除代码。

因此,在您的示例中,它比较的第一个数字是 dumphone(0) = 9942321400 当 phone(0) = 9942321400 时,它与第一次通过 Do While 时匹配。但是您的 Do While 仍在继续。当它到达第二个数字,dumphone(1) = 9865015695,它不匹配,所以你得到你的输出线。

这是您如何获得所需输出的一种选择

Dim toggle as Boolean = false
Do While (i <= ds1.Tables(0).Rows.Count - 1)
        Dim phone As String = ds1.Tables(0).Rows(i).Item(1).ToString
        Dim j As Integer = 0
        Do While (j <= Ds.Tables(0).Rows.Count - 1)
            Dim dumphone As String = Ds.Tables(0).Rows(j).Item(4).ToString
            If dumphone = phone Then toggle = true 'This will set your flag to add the output.
            j = (j + 1)
        Loop
            'After we're done checking if there's a match, we decided to add it to the output.
            If toggle = true Then
                TextBox1.AppendText(a.ToString & "|" & b.ToString & "|" & c.ToString)
                sw.WriteLine(TextBox1.Text)
                TextBox1.Text = ""
                toggle = false 'Reset the flag for the next value
            End If
            i = (i + 1) 'Move to the next value to check against.
    Loop
于 2012-07-18T06:10:46.240 回答
1

我认为,您需要比较数据集和两者共有的数字不应该出现在您的文本框中。

如果数字相等,则设置一个标志,如果未设置该标志,则仅将值写入文本框。

于 2012-07-18T06:14:17.153 回答