vb.net的初学者.正在处理登录表单


Imports MySql.Data.MySqlClient
Public Class Form1
Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
Dim con As MySqlConnection = JOKENCONN()
Public Function JOKENCONN() As MySqlConnection
Return New MySqlConnection("server=localhost; user id=root; password=; database =studentdb")
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GroupBox1.Enabled = False
End Sub
Private Sub LBLLOGIN_CLICK(sender As Object, e As EventArgs) Handles lbllogin.Click
lbllogin.Text = "Login"
lbllogin.Text = "Login"
lblname.Text = "Hi, Guest"
If lbllogin.Text = "Login" Then
GroupBox1.Enabled = True
End If
End Sub
Private Sub BTNOK_CLICK(sender As Object, e As EventArgs) Handles btnok.Click
Dim Sql As String
Dim publictable As New DataTable
Try
If txtusername.Text = "" And txtpass.Text = "" Then
MsgBox("Password or username is incorrect!")
Else
Sql = "select ' from tbluseraccount where username='" & txtusername.Text & "' and userpassword='" & txtpass.Text & "'"
With cmd
.Connection = con
End With
da.SelectCommand = cmd
da.Fill(publictable)
If publictable.Rows.Count > 0 Then
Dim user_type As String
user_type = publictable.Rows(0).Item(4)
Name = publictable.Rows(0).Item(1)
If user_type = "Admin" Then
MsgBox("Welcome " & Name & "you login as Administrator")
lbllogin.Text = "logout"
lblname.Text = "Hi, " & Name
GroupBox1.Enabled = False
txtusername.Text = ""
txtpass.Text = ""
ElseIf user_type = "cetakoradi2" Then
MsgBox("Welcome " & Name & "you login as cetakoradi2")
lbllogin.Text = "logout"
lblname.Text = "Hi, " & Name
GroupBox1.Enabled = False
txtusername.Text = ""
txtpass.Text = ""
Else
End If
Else
MsgBox("contact administrator to register")
txtusername.Text = ""
txtpass.Text = ""
End If
da.Dispose()
End If
Catch ex As Exception
MsgBox(ex.Message)
con.Close()

End Try
End Sub
End Class

这是我收到的错误

ExecuteReader CommandText属性未正确初始化

我真的需要帮助。这就是我收到的错误。谢谢

假设publictable.Rows(0).Item(4)中表示的字段名称命名为user_type,则可以使用以下内容:

'Declare the object that will be returned from the command
Dim user_type As String
'Declare the connection object
Dim con As OleDbConnection
'Wrap code in Try/Catch
Try
'Set the connection object to a new instance
con = JOKENCONN()
'Create a new instance of the command object
Using cmd As OleDbCommand = New OleDbCommand("SELECT user_type FROM tbluseraccount WHERE username=@0 AND userpassword=@1;", con)
'Paramterize the query
cmd.Parameters.AddWithValue("@0", txtusername.Text)
cmd.Parameters.AddWithValue("@1", txtpass.Text)
'Open the connection
con.Open()
'Use ExecuteScalar to return a single value
user_type = cmd.ExecuteScalar()
'Close the connection
con.Close()
End Using
Catch ex As Exception
'Display the error
Console.WriteLine(ex.Message)
Finally
'Check if the connection object was initialized
If con IsNot Nothing Then
If con.State = ConnectionState.Open Then
'Close the connection if it was left open(exception thrown)
con.Close()
End If
'Dispose of the connection object
con.Dispose()
End If
End Try
If (String.IsNullOrWhitespace(user_type)) Then
'Failed login
ElseIf (user_type = "Admin") Then
'Admin login
ElseIf (user_type = "cetakoradi2") Then
'cetakoradi2 login
Else
'Not a failed login, but also not an admin or cetakoradi2 either
End If

这段代码所做的是设置一个参数化查询,只获取用户名和密码与参数化值匹配的user_type。由于应该只有一条记录符合这些条件(大概(,因此我们可以使用ExecuteScalar只返回单个字段值。

只是为了强调这一点,MySqlCommand.ExecuteScaler和Microsoft的同行一样,"执行查询,并返回查询返回的结果集中第一行的第一列。忽略多余的列或行">并返回

@David提出的代码使用IsNullOrWhitespace检查此条件。

ExecuteScalar是有效的,但一次只检索一个值。OP追求的另一种选择是返回datarow,如果他想同时返回多个字段,这是一种有效的方法。在他的示例中,他分别检索变量user_typeName的两个字段。

注意,VB.net和其他任何编程语言一样,都保留了关键字。如果您不习惯使用良好的命名约定,那么有一天您可能会偶然发现其中一个关键字,可能会遇到晦涩难懂的错误。Name不是一个好的变量名称,并且由于每个对象都有一个name属性,因此可能会引起混淆。

为了解决手头的特定问题,错误消息ExecuteReader CommandText property has not been properly initialized是不言自明的。应该做的只是:

With cmd
.Connection = con
.CommandText = Sql
End With

您定义了一个命令,但没有告诉它该做什么。在代码中,变量Sql已定义但未使用。有了这个缺失的部分,代码就有可能按预期工作。


小细节:

不重要,但如果你输入空白,他的条件就不起作用,例如:

If txtusername.Text = "" And txtpass.Text = "" Then

一个改进是简单地修剪文本框中的值

If txtusername.Text.Trim = "" And txtpass.Text.Trim = "" Then

但我认为你想要的不是And,而是Or。我认为你不想允许没有密码的登录。


您可以使用Select Case而不是进行多个If/ElseIf

最新更新