【问题标题】:C# : how to get database column value that depends on a IDC#:如何获取依赖于 ID 的数据库列值
【发布时间】:2018-10-08 02:09:17
【问题描述】:

我很难解决它并尝试了很多次,但仍然无法正常工作。这是场景,我有一个包含用户名和密码的登录表单。我有一个用于创建用户的数据库,用户类型为管理员和员工。我想要发生的是获取用户的用户名和用户类型并将其传递给另一种形式的标签。

这是我的代码

    private static int count = 0;   

    private void btn_login_Click(object sender, EventArgs e)
    {
        using (var con = SQLConnection.GetConnection())
        {
            var selectCommand = new SqlCommand("Select * from Users_Profile where Username= @Username and Password= @Password", con);           
            selectCommand.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = txt_username.Text;
            selectCommand.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = txt_password.Text;

            SqlDataReader dataReader;
            dataReader = selectCommand.ExecuteReader();

            var loginSuccess = false;

            while (dataReader.Read())
            {
                loginSuccess = true;
            }

            if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))
            {
                MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (loginSuccess)
                {
                    count = 0;
                    MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    var obj = new MainForm(this);
                    obj.Closed += (s, args) => this.Close();
                    obj.Show();
                }
                else
                {
                    count += 1;

                    if (count == 3)
                    {
                        MetroMessageBox.Show(this, "You have exceeded maximum login attempts, Please wait 10 seconds", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        txt_username.Enabled = false;
                        txt_password.Enabled = false;
                        btn_login.Enabled = false;
                        LoginAttempstimeOut.Start();
                    }
                    else
                    {
                        MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }

                }
            }
        }        
    }

    private void LoginAttempstimeOut_Tick(object sender, EventArgs e)
    {
        LoginAttempstimeOut.Stop();
        txt_username.Enabled = true;
        txt_password.Enabled = true;
        btn_login.Enabled = true;
        count = 0;
    } 

【问题讨论】:

  • @Z.R.T.很明显。这就是他在这里问的原因。如果你回答他的问题会更好:)
  • @Z.R.T.我知道我只是忘记了,我把它用在了我的其他代码中。

标签: c# winforms


【解决方案1】:

我同意匿名的回答,但我相信您可以做一些事情来清理您的代码。

首先,您为什么要在验证您有要传递的值之前查询数据库?首先检查文本框中的值,如果有值,请进行数据库调用。

第二。我会将所有逻辑封装在一个单独的方法中,该方法返回适当的值并从 _Click() 方法中调用它。

private static int count = 0;   

private void btn_login_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))
    {
        MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {
        string Results = CheckLogin(txt_username.Text, txt_password.Text)
        //**  code to handle results from db query **//
        if (Results.Equals("Invalid"))
        {
            // handle bad password
            count++;
        }
        else
        {
            // handle good password
        }
    }    
}


private string CheckLogin(string User, string Pass)
{
    string returnstring = "Invalid";
    using (var con = SQLConnection.GetConnection())
    {
        var selectCommand = new SqlCommand("Select * from Users_Profile where Username= @Username and Password= @Password", con);           
        selectCommand.Parameters.AddWithValue("@Username", User);
        selectCommand.Parameters.AddWithValue("@Password", Pass);
        SqlDataReader dataReader;
        dataReader = selectCommand.ExecuteReader();

        var loginSuccess = false;
        while (dataReader.Read())
        {
            loginSuccess = true;
            returnstring = dataReader["Usertype"].ToString();
        }
    return returnstring;
}

我对我的代码非常熟悉,所以我什至可以将好/坏密码尝试的处理放在不同的方法中。看起来需要做很多额外的工作,但它尊重使您的方法/函数尽可能小而简洁的原则,并且只在这些方法中做一件事。

另外,还要多说一些::) 切勿在生产代码中使用“SELECT *”!就像从来没有一样。

还有一件事:如果您有权访问 SQL 服务器来创建存储过程,那么整个 SQL 命令应该在一个返回适当值的存储过程中。

我知道您会在网上找到数百万不使用存储过程的示例。这并不意味着它是正确的。 :) 使用存储过程有很多充分的理由,这是 Microsoft 推荐的(至少以前是这样)。

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 2018-09-02
    • 2015-08-08
    • 2020-01-13
    • 2021-11-09
    • 2019-12-03
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多