我正在创建一个简单的登录名(用户,密码和权限级别;1-普通用户2-管理员)
因此,我的表只有 4 列(ID、用户、密码和 lvl)。我已经有查询要在登录中输入,我想要的是创建一个"if"/条件来打开表单。例如,如果您输入信息并且您是普通用户,则会显示"Normal_user"表单,但如果您是管理员,则会显示"管理员"表单。没有权限级别,我的实际代码是:
try
{
MySqlConnection c = new MySqlConnection("Server=localhost; database=SOIS; UID=root; Pwd=; ");
MySqlCommand cmd = new MySqlCommand("Select * from usuarios where empleado_id = '" + username.Text + "' and password = '" + password.Password + "'", c);
MySqlDataReader lectura;
c.Open();
lectura = cmd.ExecuteReader();
int count = 0;
while (lectura.Read()) {
count = count + 1;
}
if (count == 1)
{
MainWindow win2 = new MainWindow();
win2.Show();
c.Close(); //In this part I think can add another query and put it into an if
this.Close();
}
else
{
c.Close();
MessageBox.Show("Wrong information");
}
c.Close();
}catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
我假设您的数据库中有第 4 列来标识管理员用户。
try
{
MySqlConnection c = new MySqlConnection("Server=localhost; database=SOIS; UID=root; Pwd=; ");
MySqlCommand cmd = new MySqlCommand("Select * from usuarios where empleado_id = '" + username.Text + "' and password = '" + password.Password + "'", c);
MySqlDataReader lectura;
c.Open();
lectura = cmd.ExecuteReader();
int count = 0;
bool isAdmin = false;
while (lectura.Read()) {
count = count + 1;
isAdmin = (lecture[3] == 1) ? true : false;
}
if (count == 1)
{
MainWindow win2 = new MainWindow();
win2.Show();
if( isAdmin ) {
// if admin open admin form
}
c.Close(); //In this part I think can add another query and put it into an if
this.Close();
}
else
{
c.Close();
MessageBox.Show("Wrong information");
}
c.Close();
}catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}