代码之家  ›  专栏  ›  技术社区  ›  thelost

带字符串列表的SQLite注入

  •  0
  • thelost  · 技术社区  · 16 年前

    有人能告诉我一种在构建查询时防止SQL注入的方法吗? SQLite 何处 WHERE 子句具有 "myval IN (string_1, ... , string_n)" 条件?

    不过,我想动态构建带有注释的命令文本,并从字符串列表中为这些注释添加参数。有更简单的方法吗?

    谢谢。

    1 回复  |  直到 16 年前
        1
  •  5
  •   Darin Dimitrov    16 年前

    不,没有更容易的方法。不要列出危险人物。只需使用带参数的命令。

    using (var conn = new SQLiteconnection(connectionString))
    using (var command = conn.CreateCommand())
    {
        conn.Open();
        command.CommandText = "select name from persons where id = @id";
        command.Parameters.AddWithValue("@id", 5);
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
    
            }
        }
    }