如何根据当前登录的用户打开不同的表单?

时间:2013-12-15 00:27:42

标签: vb.net forms

我有3种不同的形式: frmLogin.vb (这是用户将在哪里开始), frmAdminPage.vb (管理员表单), frmTeacherPage.vb (教师表格),最后 frmStudentPage.vb (学生表格)

我被要求为我的课程设计一个Spelling Bee system,但到目前为止,老师希望我根据使用“数据库”登录的用户编程如何打开不同的表单。

我正在使用的数据库是Access 2007.它包含以下字段:ID(自动编号),Username(文本),Password(文本),{{1} } (整数)。我已经有3个不同的帐户来准备这个;

我的帐户


UserAuthorization

我的代码

ID = 1, Username = Admin, Password = admin, UserAuthorization = 1

ID = 2, Username = Teacher, Password = teacher, UserAuthorization = 2

ID = 3, Username = Student, Password = student, UserAuthorization = 3

评论Imports System.Data Imports System.Data.OleDb Public Class frmLogin Dim conn As New OleDbConnection Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click 'Check if user has inputted no data If txtUsername.Text = "" Or txtPassword.Text = "" Then MessageBox.Show("Please fill in all fields.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Else 'Connect to database conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\SpellingBeeSystem.accdb" End If Try Dim sql As String = "SELECT Username, Password FROM UserAccount WHERE Username='" & txtUsername.Text & "' AND Password='" & txtPassword.Text & "'" Dim sqlCom As New OleDbCommand(sql) sqlCom.Connection = conn conn.Open() Dim sqlRead As OleDbDataReader = sqlCom.ExecuteReader If sqlRead.Read Then 'Code to check which level of authorization the user is then select the appropriate form to show Else MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) txtUsername.Text = "" txtPassword.Text = "" txtUsername.Focus() End If Catch ex As Exception MessageBox.Show("Failed to connect to databse. System Error:" & ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub End Class ;我想知道如何编写这部分代码...

我需要该程序来检查用户具有哪种级别的授权,并根据该级别打开表单。

提前致谢!!帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

我不确切知道这是否有效,但它可行,所以试一试:

代码

    Imports System.Data
    Imports System.Data.OleDb

    Public Class frmLogin

    Dim conn As New OleDbConnection

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

        'Check if user has inputted no data
        If txtUsername.Text = "" Or txtPassword.Text = "" Then
            MessageBox.Show("Please fill in all fields.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            'Connect to database
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\SpellingBeeSystem.accdb"
Try
            Dim sql As String = "SELECT Username, Password, UserAuthorization  FROM UserAccount WHERE Username='" & txtUsername.Text & "' AND Password='" & txtPassword.Text & "'"
            Dim sqlCom As New OleDbCommand(sql)

            sqlCom.Connection = conn
            conn.Open()

            Dim sqlRead As OleDbDataReader = sqlCom.ExecuteReader

            If sqlRead.Read Then
                If sqlRead[2] == 1 Then
                    frmAdminPage.Show()
                ElseIf sqlRead[2] == 2 Then
                    frmTeacherPage.Show()
                ElseIf sqlRead[2] == 3 Then
                    frmStudentPage.Show()
                End If
            Else
                MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                txtUsername.Text = ""
                txtPassword.Text = ""

                txtUsername.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show("Failed to connect to databse. System Error:" & ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        End If
    End Sub
    End Class

我做了什么

首先编辑了sql请求sql以请求行UserAuthorization。 然后检查复原数据包含的内容 - 它应该是1,2或3.根据我们收到的内容打开表单,你不会去,而且你已经完成了。

如果没有检查this有关如何使用OleDbDataReader类的更多信息,我希望这样做有效。

<强>更新

您没有将请求中止到用户没有输入任何内容的数据库,因此请求的代码必须在输入检查后的else中。