在构建SQLite
子句有WHERE
条件的"myval IN (string_1, ... , string_n)"
的查询时,有人能告诉我一种防止SQL注入的方法吗?
我想用注释动态构建命令文本,并从字符串列表中添加这些注释的参数。有更简单的方法吗?
感谢。
答案 0 :(得分:5)
不,没有更简单的方法。不要列出危险字符。只需使用带参数的命令。
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())
{
}
}
}