繁体   English   中英

Visual Basic 2015(Visual Studio)

[英]Visual Basic 2015 (Visual Studio)

我正在尝试在Visual Studio中使用Visual Basic 2015创建一个登录表单。 我已经按照观看过的视频中的说明进行了操作,但是,当我尝试运行代码时发生了错误。

这是我到目前为止完成的代码:


Private Sub picgo1_Click(sender As Object, e As EventArgs) Handles picgo1.Click

 openConn()

        Dim dr As SqlDataReader
        Dim cmd As SqlCommand
        Dim sqlsyntax As String
        cmd = New SqlCommand
        cmd.CommandType = CommandType.Text
        cmd.Connection = conn
        sqlsyntax = "select * from tblusers where user = '" & txtuser.Text & "' and pass = '" & txtpass.Text & "'"
        cmd.CommandText = sqlsyntax
        dr = cmd.ExecuteReader()

If dr.HasRows Then

 MsgBox("Access Granted! Welcome '" & txtuser.Text & "'")

Else

 MsgBox("Access Denied! Incorrect Username or Password!")

End If

 conn.Close()

 cmd.Dispose()

    End Sub

另一个模块

Imports System.Data.SqlClient

Module ModuleConnections
    Public conn As SqlConnection

    Sub openConn()
        Try
            conn = New SqlConnection("Data Source=E:\HRIMS\HRIMS V1.0\WINDOWSAPPLICATION2\HRIMSDB.MDF;Integrated security=True")
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
        Catch ex As Exception
            MsgBox("Connecting to Database Failed" & ex.ToString)
        End Try
    End Sub

End Module

当我尝试运行表单时,这是我遇到的错误 然后,当我按OK时,它将使我指向这一

我仍在尝试学习,所以请不要对我太刻苦:D

先感谢您。

尝试:

Public conn As SqlConnection

Public Sub openConn()
        conn = New SqlConnection(CONNECTIONSTRING)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        Return conn
End Function

登录代码:

 Using cmd As New SqlCommand("SELECT * 
                             FROM tblusers 
                             WHERE user = @userName 
                             AND pass = @userPass", openConn)
   'Parameterize your query to be safe from SQL Injection
   cmd.Parameters.AddWithValue("@userName", txtuser.Text)
   cmd.Parameters.AddWithValue("@userPass", txtpass.Text)
   Using rdr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult)
       Dim Login As Object = 0
       If rdr.HasRows Then
           rdr.Read
           Login = rdr(Login)
       End If
       If Login = Nothing Then
           MsgBox("Access Denied! Incorrect Username or Password!")
       Else
           'MsgBox("Access Granted! Welcome '" & txtuser.Text & "'")
           MsgBox(String.Format("Access Granted! Welcome {0}!", txtuser.text)
       End If
    End Using
End Using

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM