C# 检查数据库中的用户类型
我想检查用户是职员还是管理员。如果用户是一名职员,我想禁用或隐藏一些按钮或功能到其他窗体 loginsql中的C# 检查数据库中的用户类型,c#,sql,C#,Sql,我想检查用户是职员还是管理员。如果用户是一名职员,我想禁用或隐藏一些按钮或功能到其他窗体 loginsql中的从tblAccount中选择Username,以便我可以向其他表单显示当前用户 private void btnConfirm_Click(object sender, EventArgs e) { //DO: User authentacation con.Open(); string loginsql = "SELECT Username FROM tblAc
从tblAccount中选择Username
,以便我可以向其他表单显示当前用户
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();
}
您应该创建
再创建两个表,tblAccount已经存在,其中包含用户信息,现在再创建两个名为tblRoles和tblAccountRoles的表。 tblRoles将包含所有角色及其主键,例如1-Admin、2-staff等。 tblAccountRoles将包含分配给所有用户的角色,例如:1-Jeff-1(管理员)、2-zepp-2(职员)等等。 现在,在代码中,首先通过查询tblAccount表检查用户登录是否成功,如果成功,则检查tblAccountRoles表并获取登录用户的roleid。你将得到1个管理员,2个职员,然后你可以根据你得到的角色id编码acc。如果用户的角色id为admin(即1),则可以显示所有控件,以此类推。 你的问题是一个非常基本的问题,很容易在互联网上搜索到。多搜索一点,你就会找到很多解决方案
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”列才能使它工作
最好的方法是在数据库中创建一个角色表:
Table Roles
Role_ID | Role_Desc
关系的另一个表
Table User_Roles
User_ID | Role_ID
然后将用户ID和角色ID存储在第三个表中,并访问它以获取相应的角色。如果有任何疑问,请告诉我。考虑使用参数化命令,而不是字符串连接。