Visual Basic登录

时间:2013-10-21 05:03:51

标签: asp.net vb.net oledb executescalar data-connections

有人能帮助我,我的登录时遇到了一些问题。   当我尝试登录时,我收到错误“多步OLE DB操作生成错误。检查每个OLE DB状态值,如果可用。没有工作。” - 与ExecuteScalar检索结合。

提前感谢您提供或可能无法提供给我的任何帮助。

 Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

    If String.IsNullOrWhiteSpace(txtUsername.Text) Then
        Invalid()
        Exit Sub
    End If

    Using con As New OleDbConnection(connectionString)
        Dim cmd As New OleDbCommand("Select count(*) From tblAccounts where Username = ?", con)
        cmd.Parameters.AddWithValue("Username", txtUsername)
        con.Open()

        If CType(cmd.ExecuteScalar(), Integer) > 0 Then
            cmd.CommandText = "Select CPassword From tblAccounts Where Username = ?"
            Dim matches As String = CType(cmd.ExecuteScalar(), String)
            If matches = Sha1(txtPassword.Text) Then
                Response.Redirect("main.aspx")
            Else
                Invalid()
            End If
        Else
            Invalid()
        End If
    End Using

 End Sub

1 个答案:

答案 0 :(得分:0)

首先,我建议(已经提出建议),重新检查连接字符串......

就个人而言,我不会重复使用相同的OleDbCommand,因为这可能会导致意外结果。相反,我会尝试以下内容:

Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

    If Not String.IsNullOrWhiteSpace(txtUsername.Text) Then

        Using con As New OleDbConnection(ConnectionString)
            con.Open()

            Dim Result As Integer = 0

            Using cmd As New OleDbCommand("Select count(*) From tblAccounts where Username = @Username", con)
                cmd.Parameters.AddWithValue("@Username", txtUsername.text)

                Result = CInt(cmd.ExecuteScalar)
            End Using

            If Result > 0 Then
                Using cmd As New OleDbCommand("Select CPassword From tblAccounts Where Username = @Username", con)
                    cmd.Parameters.AddWithValue("@Username", txtUsername.text)

                    Dim Obj As Object = cmd.ExecuteScalar()

                    If (Obj IsNot Nothing) AndAlso (Obj IsNot DBNull.Value) Then
                        Dim matches As String = Obj.ToString

                        If matches = SHA1(txtPassword.Text) Then
                            Response.Redirect("main.aspx")
                            Exit Sub
                        End If

                    End If

                End Using
            End If

        End Using

    End If

    Invalid()

End Sub

希望这有帮助