【问题标题】:DataReader does not read my if codeDataReader 不读取我的 if 代码
【发布时间】:2013-09-25 21:12:55
【问题描述】:

我有一个登录winform,我在里面输入了Change Password。我有此代码用于更新我的数据库中的密码信息。但是如果它是真的它不会读取我的数据读取器,但如果它是假的它会读取它,并更改我的数据库中的密码。

    public void ChangePass()
    {
        sc.Open();
        try
        {
            if (_oldpass == "" || _newpass == "" || _conpass == "")
            {
                string message = "Must fill up all the fields!";
                string title = "Voting System Error Message";
                MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                cmd = new SqlCommand("SELECT password FROM TableLogin WHERE password = '" + _oldpass + "'", sc);

                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read() == true)
                {
                    sc.Close();
                    if (_newpass == _conpass)
                    {
                        sc.Open();
                        cmd = new SqlCommand("UPDATE TableLogin SET password = '" + _newpass + "' WHERE username = 'admin'", sc);

                        SqlDataReader sdr = cmd.ExecuteReader();
                        if (sdr.Read() == true) 
                        {
                            MessageBox.Show("Successfully Changed!"); 
//This part does not read if true.. but if sdr.Read() == false it changes the password from my database.
                        }
                    }
                    else
                    {
                        string message = "New Password and Confirm Password does not match!";
                        string title = "Voting System Error Message";

                        MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    string message = "Wrong Old Password!";
                    string title = "Voting System Error Message";

                    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }
    }

我不明白,为什么?

【问题讨论】:

  • 您的代码成熟可用于 SQL 注入。
  • Little Bobby Tables 会喜欢那个代码
  • 您正在对非标量查询执行“读取”。我不希望这能像你编码的那样工作。
  • A:SQL 注入,B:停止存储密码
  • 对不起,我是编程初学者。 @Joel,我只是想问一下,我如何根据我的代码来做呢?

标签: c# datareader


【解决方案1】:

我想 sql 中的 Update 语句不会返回记录,所以 read 不会返回 true。你应该改用ExecuteNonQuery

if (cmd.ExecuteNonQuery() > 0) 
{
    MessageBox.Show("Successfully Changed!"); 
}

在 cmets 中指出的 BTW 使用参数化查询来防止 sql 注入。

【讨论】:

  • 好的,Sriram 我现在明白了。谢谢。
【解决方案2】:

以下是CW,因为它真的是一个很大的评论。我会对您的代码进行许多更改。以下是一些重要的:

    public void ChangePass()
    {
        // Not very important, but this doesn't need to be in the try/catch
        if (_oldpass == "" || _newpass == "" || _conpass == "")
        {
            var message = "Must fill up all the fields!";
            var title = "Voting System Error Message";
            MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        try
        {
            sc.Open();
            // SqlCommand, SqlDataReader, and anything else you create that implements
            // IDisposable, needs to be in a using block
            using (var cmd = new SqlCommand("SELECT password FROM TableLogin WHERE password = @Password", sc))
            {
                // As others have said, use parameters to avoid SQL Injection Attacks
                cmd.Parameters.AddWithValue("@Password", _oldpass);

                using (var dr = cmd.ExecuteReader())
                {
                    if (dr.Read()) // You don't need == true
                    {
                        if (_newpass == _conpass)
                        {
                            // Separate SqlCommand and use a using block
                            using (
                                var updateCommand =
                                    new SqlCommand(
                                        "UPDATE TableLogin SET password = @Password WHERE username = 'admin'",
                                        sc))
                            {
                                // and a parameter
                                updateCommand.Parameters.AddWithValue("@Password", _newpass);

                                // Use ExecuteNonQuery, and check affected rows
                                var rowsAffected = updateCommand.ExecuteNonQuery();
                                if (rowsAffected == 1)
                                {
                                    MessageBox.Show("Successfully Changed!");
                                }
                            }
                        }
                        else
                        {
                            var message = "New Password and Confirm Password does not match!";
                            var title = "Voting System Error Message";

                            MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        var message = "Wrong Old Password!";
                        var title = "Voting System Error Message";

                        MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // For troubleshooting purposes, display the entire exception
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            sc.Close();
        }
    }

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多