检查登录凭据并根据用户名C#

时间:2017-02-22 19:23:11

标签: c# forms ms-access authorization

我使用此代码验证用户登录时的登录信息,这不是最多三个用户的早班,晚班和经理。

我根据用户名打开了不同的表单。我的意思是:

  • MorningShift打开frmCashier
  • EveningShift打开frmCashier3
  • Manager打开frmAdmin

我该怎么做?

在组合框中选择用户名,然后输入密码。

private void button1_Click_1(object sender, EventArgs e)
{
    string cmdText = "select Count(*) from Staff where Sname=@p1 and Spass=@p2";
    using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
    using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(cmdText, conn))
    {
        conn.Open();
        cmd.Parameters.AddWithValue("@p1", comboBox1.Text);
        cmd.Parameters.AddWithValue("@p2", textBox1.Text);  // Is this a variable or a textbox?
        int result = (int)cmd.ExecuteScalar();
        if (result > 0)
        {
            this.Hide();
            var form1 = new frmCashier();
            form1.Closed += (s, args) => this.Close();
            form1.Show();
        }
        else
            MessageBox.Show("...");
    }
}

1 个答案:

答案 0 :(得分:1)

下面的内容应该有效:

Form form1;
if (result > 0)
{
    this.Hide();
    if (comboBox1.Text.ToLower() == "MorningShift") {
        form1 = new frmCashier();
    }
    else if (comboBox1.Text.ToLower() == "EveningShift") {
        form1 = new frmCashier3();
    }
    else if (comboBox1.Text.ToLower() == "Manager") {
        form1 = new frmAdmin();
    }
    else {
        throw new Exception("unknown user");
    }
    form1.Closed += (s, args) => this.Close();
    form1.Show();
}