net字符串比较问题

net字符串比较问题

本文介绍了Vb.net字符串比较问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是当我输入我的值时,代码应该将值与查询结果进行比较并通过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

推荐答案

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


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!



这篇关于Vb.net字符串比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:41