我正在尝试在VB.NET中创建一个登录表单。我在运行表单时遇到了一些困难

时间:2012-08-14 14:55:40

标签: vb.net

我收到错误“无效播放异常未处理”。我正在使用SQL Server 2008和Visual Studio 2008.我从String“Jack”转换为类型Boolean无效。

请参阅下面的表格代码:

Imports System.Boolean

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim username As String
        Dim pswd As String
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim reader As SqlDataReader

        username = txt_username.Text
        pswd = txt_password.Text
        txt_username.Text = Focus()
        txt_password.Visible = "False"
        Try
            conn = New SqlConnection("data source=TAMIZHAN\SQLEXPRESS;persistsecurity info=False;initial catalog=log;User ID=sa;Password=123");
            cmd = New SqlCommand("Select usename,passw from Userlog where usename='" + username + "' & passw='" + pswd + "'")
            conn.Open()
            reader = cmd.ExecuteReader()
            If (String.Compare(pswd and '"+passw+"')) Then
                MsgBox("Success")
            End If
            If (reader.Read()) Then

                MsgBox("Login success")

            End If
            conn.Close()
        Catch ex As Exception
            MsgBox("Error"+ex.Message());
        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

string.Compare返回一个Integer而不是一个布尔值,应该以这种方式调用

If (String.Compare(pswd,passw) = 0) Then 
    MsgBox("Success") 
End If 

请在MSDN上see the references

但是,您的代码现在有许多问题:

  • 您正在使用字符串连接来构建您的SQL文本 (SqlInjection,引用问题)。
  • 您不使用Using语句(如果是,连接仍保持打开状态 除外)。
  • 似乎绝对不需要比较逻辑。