2013-05-01 123 views
-3

你好,我想提出两个字符串之间进行比较,首先是在给定的基础和做其他的从TextBox检索,但结果总是do检查两个字符串是否相同的VB网。

Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Text.RegularExpressions 

Public Class Form1 

    Private Sub Bcon_Click(sender As Object, e As EventArgs) Handles Bcon.Click 

     Dim cnn As SqlConnection = New SqlConnection("Data Source=BANIX;Initial Catalog=mydb;Integrated Security=True;Connect Timeout=15;Encrypt=False;") 
     Dim cmd As New SqlCommand("select * from utilisateurs", cnn) 
     Dim rd As SqlDataReader 

     Dim sr As String = vbNullString 

     Try 
      cnn.Open() 
      rd = cmd.ExecuteReader 
      While rd.Read 
       sr = rd.GetString(1) 
       RTB.AppendText(Environment.NewLine & "DB login = " & sr) 
       RTB.AppendText(Environment.NewLine & "TBLogin = " & TBlogin.Text) 
       RTB.AppendText(Environment.NewLine & "IsMatch sr:" & Regex.IsMatch(TBlogin.Text, sr)) 
       RTB.AppendText(Environment.NewLine & "Equals sr : " & String.Equals(TBlogin.Text, sr)) 

       If (TBlogin.Text = sr) Then 
        RTB.AppendText(Environment.NewLine & "Identique") 
       Else 
        RTB.AppendText(Environment.NewLine & "n'est pas Identique") 
       End If 
      End While 
      rd.Close() 

     Catch ex As Exception 
      RTB.AppendText(Environment.NewLine & " cannot connect !") 
     End Try 

     cnn.Close() 

    End Sub 

End Class 
+0

请确保您的文章都是英文的,谢谢。你还可以提供你收到的任何错误(如果有的话),你试过的东西等,等等...... – Sam 2013-05-01 09:03:21

+2

'结果总是做',这是什么意思? – 2013-05-01 09:04:31

+0

我很难理解这个“......但结果总是**'做**”。无论如何,你能显示RTB的输出吗? – ajakblackgoat 2013-05-01 09:05:11

回答

1

你必须清楚地知道什么你的意思是identical,例如在内存或逐位等值同一个字符串的引用,等于不区分大小写...

要检查两个字符串相等你用你的样品中的方式string.equals

另一种方式是String.Compare(str1,str2)之一,它将返回一个整数值,当两个字符串相同时,则此值将为0.当str1小于str2时,该值将小于零,并且当str1大于str2时,该值将大于零。这种方法有不同的重载,这允许你控制字符串的比较方式,这取决于你认为什么是等价的。 MSDN有使用示例。下面输出

不区分大小写例如“是平等的”控制台

Dim str1 As String = "TestString" 
    Dim str2 As String = "teststring" 
    If String.Compare(str1, str2, StringComparison.OrdinalIgnoreCase) = 0 Then 
     Console.WriteLine("Are Equal") 
    Else 
     Console.WriteLine("Are Not Equal") 
    End If 
+0

我已经使用这种方法,但它没有工作,我发现在SQL服务器的数据声明中的问题 谢谢你的帮助 – 2013-05-01 11:04:58

相关问题