比较VB.NET中的字符串值

时间:2009-11-13 18:49:19

标签: vb.net

如何在VB.NET中比较两个字符串值?

我尝试过比较和等于函数,但它没有给我正确的结果。我想要比较的是如下。此外,代码是否正确?

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New OleDb.OleDbConnection
        Dim ds As New DataSet
        Try
            con.ConnectionString = "Provider=SQLOLEDB;Data Source=HP-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=dbname"
            con.Open()
        Catch
            MsgBox("Error in connection")
        End Try

        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String

        sql = "select * from patientprofile"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "patientprofile")
        Dim dr As DataRow
        Dim i As Integer
        i = 0
        With ds.Tables("patientprofile")
            For Each dr In .Rows
                If String.Equals(.rows(i).Item("name"), TextBox1.Text) Then
                    textbox1.text = .rows(i).item("age")
                End If
                i = i + 1
            Next
        End With
    End Sub

End Class

3 个答案:

答案 0 :(得分:5)

为什么不

If .rows(i).Item("name").ToString() = TextBox1.Text Then
  'Other Stuff
End If

答案 1 :(得分:2)

为了绝对确定,试试这个:

with ds.Tables("patientprofile")
         For Each dr In .Rows
             if String.Equals(.rows(i).Item("name").ToString(), TextBox1.Text) then
                   textbox1.text=.rows(i).item("age").ToString()
             End If
             i = i + 1
         Next
      end with

答案 2 :(得分:-2)

使用此代码:

If (String.Compare(.rows(i).Item("name").ToString(), TextBox1.Text) = 0) Then
   ' Do something
Else
  ' Do something else
End If

我现在修复了代码。谢谢@Moayad Mardini。