这是我在堆栈溢出中的第一个问题。 我有一个出纳员系统,有两个角色,例如admin和出纳员。我需要在要摆脱角色的地方更新我的应用程序,并且当用户输入用户名和密码时,希望根据其角色来加载表单。换句话说,我不希望用户从登录名中选择角色。我正在使用Visual Studio 15和Microsoft SQL Server14。贝娄代码是登录的按钮。谢谢你。
Form2 dash = new Form2();
Form10 userdash = new Form10();
DBConnection.DBC_Connection db = new DBConnection.DBC_Connection();
DBConnection.Login lg = new DBConnection.Login();
SqlDataAdapter sda = new SqlDataAdapter("select count(*) from Login where Type='" + comboBox1Type.Text + "' and Username='" + textBox1.Text + "'and Password='" + textBox2.Text + "'", db.creatconnection());
DataTable dta = new DataTable();
sda.Fill(dta);
if (dta.Rows[0][0].ToString() == "1" && comboBox1Type.Text == "Admin")
{
this.Hide();
dash.Show();
}
else
{
if (dta.Rows[0][0].ToString() == "1" && comboBox1Type.Text == "User")
{
this.Hide();
userdash.Show();
}
else
{
MessageBox.Show("Invalid Login try checking Useraname Or Password !" , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}`
答案 0 :(得分:0)
您可以在“用户”表中使用其他列来检查角色
Create table users (
username varchar2(50) not null,
password varchar2(50) not null,
role char not null, // this takes either 0 or 1 (admin , user)
constraints PK_USERS PRIMARY KEY (username)
)
关于您的C#代码:您可以执行一个函数隐藏并显示每个角色的必要控件。
如果您有多种形式,则可以将获取的角色保存在静态变量中并进行工作。
答案 1 :(得分:0)
您可以根据凭据返回类型,然后检查返回的用户类型。
Form2 dash = new Form2();
Form10 userdash = new Form10();
DBConnection.DBC_Connection db = new DBConnection.DBC_Connection();
DBConnection.Login lg = new DBConnection.Login();
SqlDataAdapter sda = new SqlDataAdapter("select * from Login where Username='" + textBox1.Text + "'and Password='" + textBox2.Text + "'", db.creatconnection());
DataTable dta = new DataTable();
sda.Fill(dta);
if(dta.Rows.Count > 0)
{
if(dta.Rows[0]["Type"].ToString() == "Admin")
{
this.Hide();
dash.Show();
}
else
{
this.Hide();
userdash.Show();
}
}
else
{
MessageBox.Show("Invalid Login try checking Useraname Or Password !" , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
在select语句中*也将返回Type。因此,如果DataTable有任何行,则对用户进行身份验证。现在,通过检查数据表中的“类型”字段来检查该用户具有什么样的角色。