简体   繁体   English

使用连接到MS Access数据库的C#中的用户角色登录

[英]login with user roles in C# connected to MS Access database

I'm reasonably new to c# and I want some advice in relation to my login form. 我是C#的新手,我想要一些有关登录表单的建议。

Currently when a user enters their username and password it is checked in the related MS Access database and then loads the form 'Splash_Screen'. 当前,当用户输入用户名和密码时,将在相关的MS Access数据库中对其进行检查,然后加载“ Splash_Screen”格式。

However, dependant on the access level assigned to the user ie 'Admin' and 'General User' I would like it direct the user to a separate forms as I don't want 'General Users' to have the same functionality as 'Admin'. 但是,根据分配给用户的访问级别,即“管理员”和“普通用户”,我希望将用户定向到单独的表单,因为我不希望“普通用户”具有与“管理员”相同的功能。

I only have 2 types of access levels ie 'Admin' or 'General_User'. 我只有两种访问级别,即“管理员”或“ General_User”。

private void btn_Login_Click(object sender, EventArgs e)
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "select * from Account_Details where Email_Address = '"+txt_Username.Text+"' and Password = '" + txt_Password.Text + "'";

        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;
        while (reader.Read())
        {
            count = count + 1;

        }
        if (count == 1)
        {
            this.Hide();
            Splash_Screen f2 = new Splash_Screen();
            f2.Show();

        }
        else if (count > 1)
        { 
            MessageBox.Show("Duplicate Username and Password");
        }
        else
        {
            MessageBox.Show("Username and Password is not correct");
        }

        connection.Close();

    }

I have used this code to achieve the similar requirements. 我已经使用此代码来实现类似的要求。 it worked for me. 它为我工作。 i hope it will help others who have this kind of requirements. 我希望它将对有这种要求的其他人有所帮助。 i have created two textboxs named txtusername and txtpassword and one combobox for user roll and in OleDataAdapter i used a query to fetch data from users table where username and password are equal to whatever user types in textbox after that i used a if statement to check if there are any records in table than in a nested if i have checked that if comboboxroll is equal to admin than show adminform otherewise display userform. 我创建了两个名为txtusername和txtpassword的文本框,以及一个用于用户名册的组合框,并且在OleDataAdapter中,我使用查询从users表中获取数据,其中username和password等于文本框中的任何用户类型,之后我使用了if语句来检查是否如果我检查了comboboxroll是否等于admin,则表中是否有嵌套的记录;否则显示adminform,否则显示userform。

 OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [users] Where [UserName]='" + txtUserName.Text + "' And [pwd]='" + txtPassword.Text + "' And [Roll]='" + cmbroll.Text + "'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (cmbroll.Text == "Admin")
                {
                    MainFrm f = new MainFrm();
                    f.Show();
                    f.lblLogedUser.Text = txtUserName.Text;
                    this.Hide();
                }
                else {
                    MainFrmUser userform = new MainFrmUser();
                    userform.Show();
                    userform.lblLogedUser.Text = txtUserName.Text;
                    this.Hide();
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM