c# - 登录按钮的 If 语句

标签 c# visual-studio-2010 forms

我是 Visual Studio 2010 的新手。我正在创建一个登录表单,在登录表单中有一个组合框和一个文本框。组合框中的项目是员工的职位列表。每当用户单击登录按钮时,登录按钮中应该有一个 if 语句,以便在员工的特定位置打开表单。请帮忙。

这是截图:

enter image description here

这是代码:

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();
    }

我还没有包含组合框,因为我不知道该放什么。

最佳答案

我认为你需要重新考虑你的设计:

如果将用户的角色(职位)存储在数据库中会更好。

如果某人正确识别自己,则为该用户加载角色,然后打开正确的表格/应用程序。不要让用户选择他的角色。

看看the Membership and Role providers它们不仅适用于 ASP.NET,您也可以在 Winforms 中使用它们:

优秀的教程集:

关于c# - 登录按钮的 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6464374/

相关文章:

forms - 检索 Google 表单回复者的电子邮件地址,而无需在表单中询问他们的电子邮件地址

c# - system.serviceModel/bindings/wsHttpBinding 的绑定(bind)没有...错误

c# - 在Linux中使用Visual Studio C#程序

php - 尝试在文本输入后自动提交表单

c - 在 Windows 上的 VS 2010 Professional 中运行 C 项目时出现链接错误

c# - 如何使用导入的 C# 项目的应用程序配置

javascript - 使用表单的多个实例发送不同的数据

c# - 为什么不同读取方法的图像像素值不同?

c# - 在 C# (ASP.MVC) 中将 Access DB (.mdb) 与 ADODB 一起使用,就像使用经典 ASP

c# - 悬停效果对WPF TreeView的子元素有错误