无法使用存储过程VB .net

时间:2017-05-17 22:20:55

标签: .net vb.net stored-procedures windows-forms-designer

大家好我开始学习vb并尝试使用Windows窗体进行登录,但每次我尝试登录时都说用户/密码错误了...我检查了存储过程和变量的值,一切似乎确定。

在登录功能上我检查了计数值,它返回-1 ...不知道我错过了什么:/请一些帮助

这是登录按钮

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    If txtUser.Text = "" And txtPassword.Text = "" Then
        MsgBox("Fields are empty")
    Else
        If txtUser.Text = "" Then
            MsgBox("User is empty")
        Else
            If txtPassword.Text = "" Then
                MsgBox("Password is empty")
            Else
                Dim dts As New cPerson
                dts.user = txtUser.Text
                dts.pass = txtPassword.Text
                If Login(dts) = True Then
                    Me.Hide()
                    Dim box = New Menu()
                    box.Show()
                Else
                    MsgBox("user/password is wrong")
                End If
            End If
        End If
    End If
End Sub

这是登录功能

Public Shared Function Login(ByVal dts As cPerson) As Boolean
    Try
        conexion.Open()
        comando = New SqlCommand("SP_Login")
        comando.CommandType = CommandType.StoredProcedure
        comando.Connection = conexion
        comando.Parameters.AddWithValue("@usu", dts.user)
        comando.Parameters.AddWithValue("@pas", dts.pass)
        Dim cont As Integer = comando.ExecuteNonQuery()
        If (cont > 0) Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    Finally
        conexion.Close()
    End Try
End Function

最后是SP

GO
CREATE PROCEDURE SP_Login
@usu as varchar(15),
@pas as varchar(20)
as
select * from Person where user=@usu and password=@pas

1 个答案:

答案 0 :(得分:0)

您认为调用ExecuteNonQuery执行查询可能是问题的一部分吗?非查询是一个或多个不检索记录的SQL语句。你的SQL做什么?它检索记录。您应该调用ExecuteReader,然后测试HasRows属性以确定是否匹配。或者,更改您的程序员以返回计数并致电ExecuteScalar以获得该计数。