避免使用参数进行SQL注入

本文关键字:SQL 注入 参数 | 更新日期: 2023-09-27 18:00:52

我最近调整了我的代码以避免SQL注入,并在添加参数方面得到了帮助,但现在代码对我来说是半外来的,我想知道MySqlCommand需要什么对象引用。参数

MySqlParameter parameter = new MySqlParameter();
        parameter.ParameterName = "@userid";
        parameter.MySqlDbType = MySqlDbType.NVarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = this.userid_txt.Text;
        parameter.Value = this.email_txt.Text;
        parameter.Value = this.passone_txt.Text;
        parameter.Value = this.passtwo_txt.Text;
        parameter.Value = this.lastname_txt.Text;
        parameter.Value = this.firstname_txt.Text;
        string Query = "insert into userdatabase.users (userid, email, passone, passtwo, lastname, firstname) values(@userid,@email,@passone,@passtwo,@lastname_txt,@firstname)";
        MySqlCommand.Parameters.AddWithValue("@userid", this.userid_txt.Text);
        MySqlCommand.Parameters.AddWithValue("@email", this.email_txt.Text);
        MySqlCommand.Parameters.AddWithValue("@passone", this.passone_txt.Text);
        MySqlCommand.Parameters.AddWithValue("@passtwo", this.passtwo_txt.Text);
        MySqlCommand.Parameters.AddWithValue("@lastname", this.lastname_txt.Text);
        MySqlCommand.Parameters.AddWithValue("@firstname", this.firstname_txt.Text);
        MySqlConnection conDataBase = new MySqlConnection();
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlCommand cmd = new MySqlCommand(Query, conDataBase);
        cmd.ExecuteNonQuery()

我的错误状态为

"MySql.Data.MySqlClient.MySqlDbType"不包含"NVarChar"的定义

和主

非静态字段、方法或属性"MySql.Data.MySqlClient.MySqlCommand.Parameters.get"需要对象引用

我对MySql的使用相对较新,因此非常感谢您的帮助

编辑:一旦我点击连接到我的数据库的注册,就会导致致命错误的新代码

MySqlCommand cmdDataBase = new MySqlCommand();
        MySqlParameter parameter = new MySqlParameter();
        string constring = "datasource=127.0.0.1;port=3306;username=root;password=welcome";
        string Query = "insert into userdatabase.users (userid, email, passone, passtwo, lastname, firstname) values(@userid,@email,@passone,@passtwo,@lastname_txt,@firstname)";
        cmdDataBase.Parameters.Add("@userid", MySqlDbType.VarChar).Value = userid_txt.Text;
        cmdDataBase.Parameters.Add("@email", MySqlDbType.VarChar).Value = email_txt.Text;
        cmdDataBase.Parameters.Add("@passone", MySqlDbType.VarChar).Value = passone_txt.Text;
        cmdDataBase.Parameters.Add("@passtwo", MySqlDbType.VarChar).Value = passtwo_txt.Text;
        cmdDataBase.Parameters.Add("@lastname", MySqlDbType.VarChar).Value = lastname_txt.Text;
        cmdDataBase.Parameters.Add("@firstname", MySqlDbType.VarChar).Value = firstname_txt.Text;

        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmd = new MySqlCommand(Query, conDataBase);
        try {
            conDataBase.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Welcome to iDSTEM!");
        }
            catch (Exception ex)
        {
                MessageBox.Show(ex.Message);
            }

避免使用参数进行SQL注入

您需要命令本身-稍后将构建它:

MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);

只要把它移得更高,然后使用:

cmdDataBase.Parameters.AddWithValue(...);

现在还不清楚你为什么要创建parameter变量,因为你从来没有使用过它

实际上我不会使用AddWithValue,而是使用:

cmdDataBase.Parameters.Add("@userid", MySqlDbType.VarChar).Value = userid_txt.Text;
// etc

MySQL没有NVarChar类型,所以MySqlDbType也没有,因此您的错误是——我强烈怀疑您只想要VarChar。您不需要指定方向——默认情况下,这是隐含的"输入"。

此外,您应该使用using语句进行连接和命令:

using (var connection = new MySqlConnection(...))
{
    using (var command = new MySqlCommand(sql, connection))
    {
        ...
    }
}