我在System.NullReferenceException中出错了mysql select" COMMAND.CommandText =" select from from users where user = @user and pass = @ pass""点击按钮。
MysqlConn = New MySqlConnection(ConfigurationManager.ConnectionStrings("Mydb.My.MySettings.Mydb").ToString)
Dim READER As MySqlDataReader
Me.Cursor = Cursors.WaitCursor
MysqlConn.Open()
COMMAND.CommandText = "select * from Users where user= @user and pass= @pass"
COMMAND.Parameters.AddWithValue("@user", If(String.IsNullOrEmpty(TextBox_Username.Text), DBNull.Value, TextBox_Username.Text))
COMMAND.Parameters.AddWithValue("@pass", If(String.IsNullOrEmpty(TextBox_Password.Text), DBNull.Value, TextBox_Password.Text))
COMMAND = New MySqlCommand(COMMAND.CommandText, MysqlConn)
READER = COMMAND.ExecuteReader
有什么问题?
编辑:
这是完整的代码按钮:
MysqlConn = New MySqlConnection(ConfigurationManager.ConnectionStrings("Mydb.My.MySettings.Mydb").ConnectionString)
Dim READER As MySqlDataReader
Me.Cursor = Cursors.WaitCursor
MysqlConn.Open()
COMMAND.CommandText = "select * from Users where user= @user and pass= @pass"
COMMAND.Parameters.AddWithValue("@user", If(String.IsNullOrEmpty(TextBox_Username.Text), DBNull.Value, TextBox_Username.Text))
COMMAND.Parameters.AddWithValue("@pass", If(String.IsNullOrEmpty(TextBox_Password.Text), DBNull.Value, TextBox_Password.Text))
READER = COMMAND.ExecuteReader
Dim count As Integer
count = 0
While READER.Read
count = count + 1
End While
If count = 1 Then
MessageBox.Show("Usuario y Contraseña Correctos")
Dim role As String = READER("tipo")
If role = "Administrador" Then
Inicio.Show()
ElseIf role = "Calidad" Then
Calidad.Show()
ElseIf role = "Almacen" Then
Almacen.Show()
ElseIf role = "Oficina" Then
Oficina.Show()
ElseIf role = "Minas" Then
Minas.Show()
End If
Me.Hide()
ElseIf count > 1 Then
MessageBox.Show("Usuario y Contraseña Duplicados")
Else
MessageBox.Show("Usuario y Contraseña Incorrectos")
End If
MysqlConn.Close()
MysqlConn.Open()
COMMAND.CommandText = "insert into Registro_Login (user,fecha,ip) values (@user,@fecha,@ip)"
COMMAND.Parameters.AddWithValue("@user", If(String.IsNullOrEmpty(TextBox_Username.Text), DBNull.Value, TextBox_Username.Text))
COMMAND.Parameters.AddWithValue("@fecha", If(String.IsNullOrEmpty(Label4.Text), DBNull.Value, Label4.Text))
COMMAND.Parameters.AddWithValue("@ip", If(String.IsNullOrEmpty(Label3.Text), DBNull.Value, Label3.Text))
READER = COMMAND.ExecuteReader
MysqlConn.Close()
Me.Cursor = Cursors.Default
但同样的问题
答案 0 :(得分:1)
COMMAND在使用之前似乎没有定义。就像你定义了READER一样,定义COMMAND然后使用它。并且还改变下面的代码:
ConfigurationManager.ConnectionStrings("Mydb.My.MySettings.Mydb")
要
ConfigurationManager.ConnectionStrings("Mydb.My.MySettings.Mydb").ConnectionString
.ToString应该是.ToString(),因为它是一种方法。
我不确定您是否真的能够编译此代码或发布整个代码,如果您还没有这样做,建议解决方案。
答案 1 :(得分:1)
您的命令没有设置连接。
替换这个:
COMMAND.CommandText = "select * from Users where user= @user and pass= @pass"
与
COMMAND = New MySqlCommand("select * from Users where user= @user and pass= @pass", MysqlConn)