【问题标题】:How to open separate window for each user如何为每个用户打开单独的窗口
【发布时间】:2016-09-03 02:28:43
【问题描述】:

我想制作一个登录表单,为每个用户打开单独的窗口 类型。在这个程序数据库中有 3 列。(用户名、密码、用户类型)在这个程序中有 3 种用户类型(管理员、经理、用户)并且每个用户类型都有单独的窗口。

  1. admin = Form2
  2. 经理 = Form3
  3. 用户 = Form4

这是我的登录按钮代码。请帮我把这个分开打开 每个用户类型的窗口。

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\sasindu\documents\visual studio 2010\Projects\Employee Database\Employee Database\Database.mdf;Integrated Security=True;User Instance=True");
    SqlCommand cmd = new SqlCommand("select * from login where username=@username and password =@password", con);
    cmd.Parameters.AddWithValue("@username", textBox1.Text);
    cmd.Parameters.AddWithValue("@password", textBox2.Text);

    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    con.Open();
    int i = cmd.ExecuteNonQuery();

    con.Close();

    if (dt.Rows.Count > 0)
    {
        Form3 Form = new Form3();
        Form.Show();
        this.Hide();  
    }    
    else
    {
        MessageBox.Show("Please enter Correct Username and Password");
    }

【问题讨论】:

    标签: c# sql .net winforms login


    【解决方案1】:

    我认为您已经在登录表用户类型中添加了一列。 用户类型列有派生管理员、用户、经理等。 然后您将轻松验证

    if(dt.rows[0]["user_type"].tostring()=="Admin")
    {
    // which form you show
    
    }
    
    if(dt.rows[0]["user_type"].tostring()=="User")
    {
    // which form you show
    
    }
    
    if(dt.rows[0]["user_type"].tostring()=="Manager")
    {
    // which form you show
    
    }
    

    我希望这段代码对你有帮助。如果有任何疑问,请发表评论。

    【讨论】:

    • 不做if-else,将上面的内容转换成switch-case
    【解决方案2】:
    switch (dt.Rows[0]["user_type"].ToString().ToLower())
            {
                case "admin":
                //Show Admin form
                    MessageBox.Show("admin form");
                    break;
                case "user":
                //Show User form
                    MessageBox.Show("user form");
                    break;
                case "manager":
                //Show Manager form
                    MessageBox.Show("manager form");
                    break;
                default:
                    MessageBox.Show("Invalid user type.");
                    break;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2019-09-14
      • 1970-01-01
      相关资源
      最近更新 更多