我试图在中使用Visual 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键时,它就指向这个线路。
我还在努力学习,所以请不要对我太苛刻。
提前谢谢你。
发布于 2016-08-03 11:50:13
尝试:
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 Usinghttps://stackoverflow.com/questions/38735314
复制相似问题