0

登录成功后如何重定向到另一个页面?以及我将如何检查用户名和密码是否正确?

这是我的登录代码:

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click

   If txtUser.Text = "" Then
        MsgBox("Please Enter The Username !", MsgBoxStyle.OkOnly)

    ElseIf txtPass.Text = "" Then
        MsgBox("Please Enter The Password !", MsgBoxStyle.OkOnly)


    End If

    Dim connectionString As String = "server=''; user id=''; password=''; Database=''"
    Dim conLogin As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)

    Dim queryString As String = "SELECT login VALUES " _
    & "(@username, @password) "
4

4 回答 4

1

要重定向到另一个页面,请参见下面的代码

response.redirect("abc.apsx")
于 2013-05-23T08:14:27.560 回答
1

您需要先学习 SQL: 以下语句不是正确的 SQL。

SELECT login VALUES (@username, @password)

该语句应如下所示:

SELECT * FROM Login WHERE UserName = @username AND Password = @password

然后你需要设置命令参数@username@password.

我也怀疑您使用的连接字符串是否正确。使用 anSqlConnectionStringBuilder创建正确的连接字符串。此外,您没有打开连接,因此无法访问数据库。

于 2013-05-23T08:08:45.337 回答
0

您还可以使用 datareader 和这个 sql 查询

Select count(*) from Login where UserName = @username and Password = @password

if (dr > 0) {
response.redirect('home');
}
else{
//error message
}

只是搜索数据阅读器............希望它有帮助......

于 2013-10-08T06:00:08.753 回答
0

这是登录功能的完整代码。

欲了解更多详情,请访问我的博客:http ://uncopyrightables2011.blogspot.com/ 或推特我:@MaxRohana

Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
    ConnectToSQL()

End Sub

Private Sub ConnectToSQL()

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim Passowrd As String
    Dim Passowrd2 As String
    Dim userName As String

    Try
        If
            'change the data source and initial catalog according to your sql server engine and data base
            con.ConnectionString = "Data Source = YOUR-PC; Initial Catalog = YOUR-DB; Integrated Security = True"
            con.Open()

            cmd.Connection = con
            'change the data fields names and table according to your database
            cmd.CommandText = " SELECT  UserName, Password FROM   AdminDetail WHERE   (UserName = '" & txtUsername.Text & "' ) AND (Password = '" & txtPassword.Text & "')"

            Dim lrd As SqlDataReader = cmd.ExecuteReader()
            If lrd.HasRows Then
                While lrd.Read()

                    'Do something here
                    Passowrd = lrd("Password").ToString()
                    userName = lrd("UserName").ToString()

                    Passowrd2 = txtPassword.Text()

                    If Passowrd = Passowrd2 And userName = txtUsername.Text Then

                        MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information
                                        )
                        frmMain.Show()
                        Me.Hide()

                        'Clear all fields
                        txtPassword.Text = ""
                        txtUsername.Text = ""

                    End If

                End While

            Else
                MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                'Clear all fields
                txtPassword.Text = ""
                txtUsername.Text = ""
            End If

        End If

    Catch ex As Exception
        MessageBox.Show("Error while connecting to SQL Server." & ex.Message)

    Finally
        con.Close() 'Whether there is error or not. Close the connection.

    End Try

End Sub
于 2014-06-04T08:25:51.687 回答