我需要从C#代码执行动态字符串与SQL查询并防止SQL注入。防止动态代码中的SQL注入
我的代码是这样的:
internal static string Get_Running_Cars(string from, string to)
{
return string.Format(
" declare @from as datetime = '{0}'" +
" declare @to as datetime = '{1}'" +
" select top 3 DATEDIFF(second,starttime,endtime) as sum,carname as name" +
" from cars" +
" where @from < starttime and @to > endtime ", from, to
);
}
的代码,我可以恶意代码插入到这些字符串。
我该如何安全使用它? 我应该防止那些字符:'
,;
,,
-
?
将此代码移至存储过程非常容易,您可以通过传递参数轻松防止sql注入。有什么具体的要求使用动态查询? –
这里绝对没有理由使用动态查询。 – Maarten
我有超过100个需要注入的查询,因此我需要使用动态查询。 我不能接受100个查询并将其全部转换为存储过程。 是否有注入这些查询的好方法? –