Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我正在尝试在VB.NET中创建一个登录表单。I';我在运行我的表格时遇到了一些困难_Vb.net - Fatal编程技术网

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

I';我正在尝试在VB.NET中创建一个登录表单。I';我在运行我的表格时遇到了一些困难,vb.net,Vb.net,我收到错误“未处理无效强制转换异常”。我正在使用SQL Server 2008和Visual Studio 2008。从字符串“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.E

我收到错误“未处理无效强制转换异常”。我正在使用SQL Server 2008和Visual Studio 2008。从字符串“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

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

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

但是,您的代码目前存在许多问题:

  • 您正在使用字符串连接来构建sql文本 (SqlInjection,引用问题)
  • 您不使用Using语句(在发生错误时,连接保持打开状态) 例外情况)
  • 比较逻辑似乎是绝对不需要的

字符串.Compare返回一个不是布尔值的整数,应以这种方式调用

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

但是,您的代码目前存在许多问题:

  • 您正在使用字符串连接来构建sql文本 (SqlInjection,引用问题)
  • 您不使用Using语句(在发生错误时,连接保持打开状态) 例外情况)
  • 比较逻辑似乎是绝对不需要的

您是否尝试过单步执行代码以查看它在哪一行产生错误?这将有助于诊断问题。您是否尝试单步执行代码以查看它在哪一行产生错误?这将有助于诊断问题。