简体   繁体   English

If-Statement为登录按钮

[英]If-Statement for the Login Button

I'm new to Visual Studio 2010. I'm creating a login form and in the login form there is a combobox and a textbox. 我是Visual Studio 2010的新手。我正在创建一个登录表单,在登录表单中有一个组合框和一个文本框。 The items in combobox is the list of positions of the employees. 组合框中的项目是员工的职位列表。 Whenever a user click the login button there should be an if statement in the login button so that there are forms will open in a specific position of the employees. 每当用户单击登录按钮时,登录按钮中应该有一个if语句,以便在员工的特定位置打开表单。 Please help. 请帮忙。

This is the screenshot: 这是截图:

在此输入图像描述

This is the code: 这是代码:

private void loginbutton_Click(object sender, EventArgs e)
    {

        string MyConString = "SERVER=localhost;" + "DATABASE=timekeeping;" + "UID=root;" + "PASSWORD=admin;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader Reader;
        command.CommandText = "select username, password from users";
        connection.Open();
        Reader = command.ExecuteReader();
        while (Reader.Read())
        {
            if (username_login.Text == Reader[0].ToString() && password_login.Text == Reader[1].ToString().Trim())
            {
                username = Reader[0].ToString();
                password = Reader[1].ToString();
            }
        }

        if (username_login.Text == username && password_login.Text == password.Trim())
        {
            this.Hide();
            Home form = new Home();
            //form.userSession(lname, fname);
            form.Show();
        }
        else MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        connection.Close();
    }

I haven't included yet the combobox because I don't know what to put in here. 我还没有包括组合框,因为我不知道该放什么。

I think you need to reconsider your design: 我想你需要重新考虑你的设计:

It would be better if you stored the roles (position) of the users in the/a database. 如果将用户的角色(位置)存储在/ a数据库中会更好。

If someone correctly identifies himself load the role(s) for that user and then open the correct form/application. 如果有人正确识别自己加载该用户的角色,然后打开正确的表单/应用程序。 Do not let the user select his role. 不要让用户选择他的角色。

Have a look at the Membership and Role providers they're not just for ASP.NET, you can use them in Winforms as well: 看看它们不仅仅适用于ASP.NET 的成员资格和角色提供程序 ,您也可以在Winforms中使用它们:

Excellent set of tutorials: 优秀的教程:

How about something like the following: 如下所示:

        Form mainForm;

        switch (comboBox1.SelectedText)
        {
            case "Individual employees":
                mainForm = new EmployeesForm();
                break;
            case "HR":
                mainForm = new HRForm();
                break;
            case "Manager":
                mainForm = new ManagerForm();
                break;
            case "Supervisor":
                mainForm = new SupervisorForm();
                break;
            default:
                mainForm = new DefaultForm();
                break;
        }

        mainForm.Show();

This uses a conditional statement called the switch , which handles multiple selections by passing control to one of the case statements within its body. 这使用一个名为switch的条件语句,它通过将控制权传递给其主体中的一个case语句来处理多个选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM