我怎么能用户提供的输入添加到SQL语句? [英] How can I add user-supplied input to an SQL statement?

查看:153
本文介绍了我怎么能用户提供的输入添加到SQL语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用用户提供的数据的SQL语句。我用与此类似code:

I am trying to create an SQL statement using user-supplied data. I use code similar to this:

var sql = "INSERT INTO myTable (myField1, myField2) " +
          "VALUES ('" + someVariable + "', '" + someTextBox.Text + "');";

var cmd = new SqlCommand(sql, myDbConnection);
cmd.ExecuteNonQuery();

然而,


  • 此,当用户输入中包含单引号失败(例如奥布莱恩),

  • 我似乎无法插入日期时间值时,为了获得正确的格式和

  • 的人都跟我说我不应该这样做,因为SQL注入的这一点。

我如何做正确的方式?

推荐答案

使用参数化的SQL。

例子

@ ... 占位符替换您的字符串连接,并且事后,值添加到您的SqlCommand。您可以选择占位符的名称随意,只要确保他们开始与 @ 标志。你的榜样应该是这样的:

Replace your string concatenations with @... placeholders and, afterwards, add the values to your SqlCommand. You can choose the name of the placeholders freely, just make sure that they start with the @ sign. Your example would look like this:

var sql = "INSERT INTO myTable (myField1, myField2) " +
          "VALUES (@someValue, @someOtherValue);";

using (var cmd = new SqlCommand(sql, myDbConnection))
{
    cmd.Parameters.AddWithValue("@someValue", someVariable);
    cmd.Parameters.AddWithValue("@someOtherValue", someTextBox.Text);
    cmd.ExecuteNonQuery();
}

相同的模式被用于其他类型的SQL语句:

The same pattern is used for other kinds of SQL statements:

var sql = "UPDATE myTable SET myField1 = @newValue WHERE myField2 = @someValue;";

// see above, same as INSERT

var sql = "SELECT myField1, myField2 FROM myTable WHERE myField3 = @someValue;";

using (var cmd = new SqlCommand(sql, myDbConnection))
{
    cmd.Parameters.AddWithValue("@someValue", someVariable);
    using (var reader = cmd.ExecuteReader())
    {
        ...
    }
    // Alternatively: object result = cmd.ExecuteScalar();
    // if you are only interested in one value of one row.
}

一个忠告: AddWithValue 是一个很好的出发点和工作在大多数情况下的罚款。但是,您在需要传递的值,以精确地匹配相应的数据库字段的数据类型。否则,可能会在转换$ P $从使用索引 pvents您的查询的情况下结束。请注意,某些SQL Server数据类型,如CHAR / VARCHAR(不含precedingN)或日期没有相应的.NET数据类型。在这种情况下, 添加用正确的数据类型,应使用

A word of caution: AddWithValue is a good starting point and works fine in most cases. However, that the value you pass in needs to exactly match the data type of the corresponding database field. Otherwise, you might end up in a situation where the conversion prevents your query from using an index. Note that some SQL Server data types, such as char/varchar (without preceding "n") or date do not have a corresponding .NET data type. In those cases, Add with the correct data type should be used instead.

我为什么要那样做?

  • It's more secure: It stops SQL injection. (Bobby Tables won't delete your student records.)

它更容易:无需摆弄单引号和双引号或查找正确的字符串重新日期文字的presentation

It's easier: No need to fiddle around with single and double quotes or to look up the correct string representation of date literals.

这是更稳定:奥布莱恩只是因为他在保持自己陌生的名字坚称不会崩溃您的应用程序

It's more stable: O'Brien won't crash your application just because he insists on keeping his strange name.

其他数据库访问的库


  • 如果你使用一个OleDbCommand代替的SqlCommand(例如,如果您使用的是MS Access数据库),使用而不是 @ ... 在SQL中的占位符。在这种情况下,第一个参数 AddWithValue 是无关紧要的;相反,你必须按照正确的顺序添加参数。 同样是如此的OdbcCommand

  • If you use an OleDbCommand instead of an SqlCommand (e.g., if you are using an MS Access database), use ? instead of @... as the placeholder in the SQL. In that case, the first parameter of AddWithValue is irrelevant; instead, you need to add the parameters in the correct order. The same is true for OdbcCommand.

实体框架还支持参数化查询。

这篇关于我怎么能用户提供的输入添加到SQL语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆