检查数据库中的用户类型

时间:2015-09-06 10:43:18

标签: c# sql

我想检查用户是员工还是管理员。如果用户是我的工作人员,我想禁用或隐藏某些按钮或功能到其他表单。

loginsql中的SELECT Username FROM tblAccount,以便我可以显示谁是当前用户的其他表单。

private void btnConfirm_Click(object sender, EventArgs e)
{
    //DO: User authentacation
    con.Open();
    string loginsql = "SELECT Username FROM tblAccount WHERE Username = '" + txtUser.Text + "' COLLATE SQL_Latin1_General_CP1_CS_AS AND Password = '" + txtPass.Text + "' COLLATE SQL_Latin1_General_CP1_CS_AS AND EmpStatus = 'Active'";
    SqlDataAdapter loginda = new SqlDataAdapter(loginsql, con);
    DataTable logindt = new System.Data.DataTable();
    loginda.Fill(logindt);

    if (logindt.Rows.Count == 1)
    {
        MessageBox.Show("Login Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        HomeForm home = new HomeForm(logindt.Rows[0][0].ToString());
        home.Show();
        this.Hide();
    }
    else if (txtUser.Text == "" && txtPass.Text == "")
    {
        MessageBox.Show("Enter your username and password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else if(txtUser.Text == "")
    {
        MessageBox.Show("Enter your username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else if(txtPass.Text == "")
    {
        MessageBox.Show("Enter your password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        MessageBox.Show("Invalid Username and Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtUser.Clear();
        txtPass.Clear();
        txtUser.Focus();
    }
    con.Close();
}

3 个答案:

答案 0 :(得分:0)

你应该创建

  1. 角色表 id-> foreignKey to loginsql的Id 角色(类型为布尔值) - >例如Staff = 0或Admin = 1

  2. 主持表为用户提供删除插入更新等访问权限的表 当用户登录时,你现在可以自己的角色(管理员或工作人员?)并且可以访问他们看到这个页面了吗?

答案 1 :(得分:0)

创建另外两个表,tblAccount已经存在,其中包含用户的信息,现在再创建两个名为tblRoles&的表。 tblAccountRoles。 tblRoles将包含其主键的所有角色,例如1-Admin,2-staff等。 tblAccountRoles将包含分配给所有用户的角色,例如:1-Jeff-1(admin),2-zepp -2(staff)等等。 现在在您的代码中首先通过查询tblAccount表来检查用户的登录是否成功,如果成功,则检查tblAccountRoles表并获取登录用户的roleid。您将获得1个管理员,2个工作人员,然后您可以根据您获得的角色ID进行编码。如果用户的角色ID是admin,则可以显示所有控件,依此类推。 您的问题非常基本,可以在互联网上轻松搜索。再做一点搜索,你会发现很多解决方案。

答案 2 :(得分:0)

    string loginsql = "SELECT Username, UserType FROM tblAccount WHERE Username = '" + txtUser.Text + "' COLLATE SQL_Latin1_General_CP1_CS_AS AND Password = '" + txtPass.Text + "' COLLATE SQL_Latin1_General_CP1_CS_AS AND EmpStatus = 'Active'";
    SqlDataAdapter loginda = new SqlDataAdapter(loginsql, con);
    DataTable logindt = new System.Data.DataTable();
    loginda.Fill(logindt);

    if (logindt.Rows.Count == 1)
        {
            Session["UserType"] = logindt.Rows[0][1].ToString();
            MessageBox.Show("Login Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            HomeForm home = new HomeForm(logindt.Rows[0][0].ToString());
            home.Show();
            this.Hide();
        }

如果要使用UserType隐藏某些信息,请使用以下命令:

string userType = Session["UserType"].ToString();
if(userType == "Staff")
{
//Show Some data related to Staff
}
else if (userType == "Admin")
{
//Show Some Data related to Admin
}

我还没有对代码进行测试,只是将我的代码添加到您现有代码中,所以请进行测试并告诉我。但是我给出了如何使用现有表等实现它的基本概念。但是你必须拥有' UserType'数据库表中的列使其工作。

最好的方法是在数据库中创建一个Role表:

Table Roles
Role_ID | Role_Desc

关系的另一个表

Table User_Roles
User_ID | Role_ID

然后将User_ID和Role_ID存储在第三个表中并访问它以获取相应的角色。如有任何疑问,请与我联系。