Vb.net字符串比较问题 [英] Vb.net string comparasion issue
问题描述
问题是当我输入我的值时,代码应该将值与查询结果进行比较并通过if语句运行。它的作用绝对没有。它没有错误,它没有显示任何警告。
我尝试过:
The problem is when I input my value the code is supposed to compare the value against a query result and run thru the if statement. What it is doing is absolutely nothing. It does not error out, it does not display any warnings.
What I have tried:
Using fetchconn As New OleDbConnection(connStr)
Using cmdfetch As New OleDbCommand()
cmdfetch.Connection = fetchconn
cmdfetch.CommandText = "Select AssetBarcode from TrackingInfo Where AssetBarcode = ? AND CheckedOut = True"
cmdfetch.Parameters.AddWithValue("AssetBarcode", ChkInBarcode.Text)
fetchconn.Open()
Using fetchreader As OleDbDataReader = cmdfetch.ExecuteReader(CommandBehavior.CloseConnection)
While fetchreader.Read()
If String.Equals(ChkInBarcode.Text, fetchreader("AssetBarcode"), StringComparison.OrdinalIgnoreCase) Then
'Do Nothing
Else
MessageBox.Show("This item has not been checked out. Please inform the Helpdesk")
Me.Close()
Return
End If
End While
End Using
End Using
End Using
推荐答案
问题非常明显:
- 您的查询只返回
AssetBarcode
的行column等于ChkInBarcode.Text
;- 然后循环遍历这些行,如果
AssetBarcode
列等于ChkInBarcode .Text
,你什么都不做;- 如果没有匹配的行,那你也什么都不做;
The problem is fairly obvious:
- Your query returns only those rows where the
AssetBarcode
column is equal to theChkInBarcode.Text
;- You then loop over those rows, and if the
AssetBarcode
column is equal to theChkInBarcode.Text
, you do nothing;- If there are no matching rows, then you also do nothing;
Using fetchconn As New OleDbConnection(connStr)
Using cmdfetch As New OleDbCommand()
cmdfetch.Connection = fetchconn
cmdfetch.CommandText = "Select CheckedOut from TrackingInfo Where AssetBarcode = ?"
cmdfetch.Parameters.AddWithValue("AssetBarcode", ChkOutBarcode.Text)
fetchconn.Open()
Dim result As Object = cmdfetch.ExecuteScalar()
If result Is Nothing Then
MessageBox.Show("This item does not exist. Please inform the Helpdesk")
Me.Close()
Else If Not CBool(result) Then
MessageBox.Show("This item has not been checked out. Please inform the Helpdesk")
Me.Close()
End If
End Using
End Using
首先使用调试器找出发生了什么:在行上放置一个断点
Start by using the debugger to find out what is going on: put a breakpoint on the line
fetchconn.Open()
然后继续你的步骤代码通过。
最有可能的是,你会发现从未输入而
循环,这意味着没有符合你条件的行;或者SQL命令失败并发生异常。
为什么? Dunno - 我们无法找到,因为我们无法访问您的数据,因此我们无法在您执行的相同条件下运行测试。因此,您可以使用调试器并确切了解发生了什么。
但是这段代码有点傻:你的SQL可能只返回一个COUNT(*)而不是行集,并使用ExecuteScalar检查是否有任何行符合您的条件。由于你的
如果
只检查与 WHERE
子句相同的字符串,那么它是多余的无论如何要再次检查!我先找出发生了什么,修复它,然后将它重构为更简洁,更简单的代码!
And step your code through.
Most likely, you will find that the while
loop is never entered, which means that no rows matched your criteria; or that the SQL command failed and an exception occurred.
Why? Dunno - and we can't find out because we don't have any access to your data, so we can't run the tests under the same conditions you do. So it's going to be up to you to use the debugger and find out exactly what is happening.
But that code is a bit silly: Your SQL could just return a COUNT(*) instead of a row set, and use ExecuteScalar to check if any rows match your condition. Since your
if
only checks for the same string as your WHERE
clause, it's redundant to check it again anyway! I'd find out what is happening first, fix it, and then refactor that to a lot cleaner, simpler code!
有一个工具可以让你看到你的代码正在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。
当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器。
使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。
调试器 - 维基百科,免费的百科全书 [ ^ ]
Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]
初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。
调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
这篇关于Vb.net字符串比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!