我有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手机,我们必须在记事本中写入该数据集电话号码。但是我得到了错误的结果。
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
答案 0 :(得分:1)
您的代码正在比较第二个数据集的每一行与第一个数据集中的每一行。
你的Do While块经历了整个事情,它没有排除代码。
所以在你的例子中,它比较的第一个数字是dumphone(0)= 9942321400它与第一次通过Do时匹配当电话(0)= 9942321400时。但是你的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
答案 1 :(得分:1)
我认为,您需要比较数据集和两者共有的数字不应出现在文本框中。
如果数字相等则设置一个标志,如果没有设置该标志,则只将值写入文本框。