我需要使用适用于log porposes的参数进行sql查询。
private dynamic GetInfo(int cdEmpresa)
{
dynamic info = new ExpandoObject();
StringBuilder sql = new StringBuilder();
sql.AppendLine(" SELECT * from FROM EMPRESA E");
sql.AppendLine(" WHERE cdEmpresa = @cdEmpresa ");
using (IDbConnection cn = GetConnection(cdEmpresa).Connection)
{
Logger.Debug("SQL: " + sql.ToString()); // Does not apply the parameters, obviously
cn.Open();
info = cn.Query<dynamic>(sql.ToString(), new
{
cdEmpresa = cdEmpresa // i need to execute te sql to parametrize it, is there a way to parametrize it first its execution?
}).ToList();
}
return infoCadastro;
}
答案 0 :(得分:2)
在处理查询时,的任何时候都不存在 。
即使执行查询,参数值从不直接替换为SQL命令。参数化查询的重点是代码是代码,数据是数据,两者永远不会交叉。这消除了注入攻击的任何可能性,无论您可能需要处理什么新的语言功能,自定义转义字符或unicode怪异。
在服务器端,而不是:
SELECT * FROM [table] WHERE ID=1234;
就像你运行这样的代码一样:
DECLARE @ID int;
SET @ID = LoadParameterValueFromClientQueryObject("ID");
SELECT * FROM [table] WHERE ID= @ID;
你 总是处理变量,其中变量的值远离sql语言编译器/优化器,直到命令已经编译成执行计划为止。< / p>