C# 过滤DataGrid时消除SQL注入的可能性
我有来自SQL数据库的数据的WPF表单。我从SQL中获取数据,将其插入DataTable,然后将其传递给WPF DataGrid。我添加了几个文本框用于过滤目的 我得到一张桌子:C# 过滤DataGrid时消除SQL注入的可能性,c#,wpf,C#,Wpf,我有来自SQL数据库的数据的WPF表单。我从SQL中获取数据,将其插入DataTable,然后将其传递给WPF DataGrid。我添加了几个文本框用于过滤目的 我得到一张桌子: public Task<DataView> LoadMainTableDataAsync() { return Task.Run(() => { MainProcess.MergedTable(); re
public Task<DataView> LoadMainTableDataAsync()
{
return Task.Run(() =>
{
MainProcess.MergedTable();
return MainProcess.Customers.DefaultView;
});
}
这是我目前的代码:
private readonly Dictionary<string, string> _conditions = new Dictionary<string, string>();
public void FilterSetup()
{
try
{
string qry = null;
_conditions["name"] = null;
if (!string.IsNullOrEmpty(BusinessIDSearch.Text))
{
qry = string.Format("LY Like '{0}%'", BusinessIDSearch.Text);
}
if (!string.IsNullOrEmpty(NameSearch.Text))
{
if (!string.IsNullOrEmpty(qry))
qry += " AND ";
qry += string.Format("HAKUNIMI Like '%{0}%'", NameSearch.Text);
}
if (!string.IsNullOrEmpty(GroupSearch.Text))
{
if (!string.IsNullOrEmpty(qry))
qry += " AND ";
qry += string.Format("KONSERNI Like '{0}%'", GroupSearch.Text);
}
if (!string.IsNullOrEmpty(IDSearch.Text))
{
if (!string.IsNullOrEmpty(qry))
qry += " AND ";
qry += string.Format("YRNRO Like '{0}%'", IDSearch.Text);
}
_conditions["name"] = qry;
UpdateFilter();
LiinosFICount.Content = DataGrid1.Items.Count;
}
catch (Exception)
{
throw;
}
}
我主要关心的是在这个设置中SQL注入的可能性
然而,我也面临着另一个问题,虽然键入文本框过滤效果良好,但在删除文本时,我注意到在数据网格中更新数据之前,我必须删除3个字符。所以在美国的情况下,在我的DataGrid更新之前,我按了3次backspace,所以只有当我看到Amer DataGrid数据更新时。为什么会这样
编辑:
Updatefilter方法:
private void UpdateFilter()
{
try
{
var activeConditions = _conditions.Where(c => c.Value != null).Select(c => "(" + c.Value + ")");
DataView dv = DataGrid1.ItemsSource as DataView;
dv.RowFilter = string.Join(" AND ", activeConditions);
}
catch (Exception)
{
//MessageBox.Show(ex.Message);
}
}
编辑2:
我主要关心的是在这个设置中SQL注入的可能性
只要使用参数占位符而不是将文本框中的实际值插入字符串中,就可以动态生成查询字符串本身。还应使用StringBuilder,而不是串联字符串:
SqlCommand command = new SqlCommand();
StringBuilder sb = new StringBuilder();
...
if (!string.IsNullOrEmpty(NameSearch.Text))
{
if (!string.IsNullOrEmpty(qry))
sb.Append(" AND ");
sb.Append("HAKUNIMI Like '%' + @NameSearch + '%'");
command.Parameters.AddWithValue("NameSearch", NameSearch.Text);
}
...
command.CommandText = sb.ToString():
有关如何在SQL查询中使用参数的详细信息和完整示例,请参阅
关于您的另一个问题,每次击键时都应该触发TextChanged事件。问题:参数化语句不是选项吗?如果是,为什么?因为这是一种非常快速、简单、可靠的数据库保护方法。@Franzgleichman我知道这种方法还不太专业,还在学习中。我记得在从数据库获取数据时使用了参数化语句。然而,我不确定在DataGrid中过滤数据时应该如何做。还有,这是我正在做的正确的方式吗?我也知道MVVM,但这是我学习WPF的下一步。请向我们展示你的UpdateFilter方法。@LajosArpad我已将它添加到我的问题中。如果你不发布SQL,它不可能是SQL注入。。。可能会在过滤器中查询比您预期的更多的内容,但是:他们肯定不会做比这更恶意的事情。不过,不得不说:将东西加载到数据表中很少是正确的选择。谢谢!我很难让它工作,请看我的编辑2在原来的问题。我不确定我做错了什么?我是否以正确的方式将某人分配给_条件[姓名]?
private void UpdateFilter()
{
try
{
var activeConditions = _conditions.Where(c => c.Value != null).Select(c => "(" + c.Value + ")");
DataView dv = DataGrid1.ItemsSource as DataView;
dv.RowFilter = string.Join(" AND ", activeConditions);
}
catch (Exception)
{
//MessageBox.Show(ex.Message);
}
}
public void FilterSetup()
{
SqlCommand command = new SqlCommand();
StringBuilder sb = new StringBuilder();
_conditions["name"] = null;
try
{
if (!string.IsNullOrEmpty(BusinessIDSearch.Text))
{
sb.Append("LY Like '%' + @BusinessIDSearch + '%'");
command.Parameters.AddWithValue("BusinessIDSearch", BusinessIDSearch.Text);
}
if (!string.IsNullOrEmpty(NameSearch.Text))
{
if (!string.IsNullOrEmpty(sb.ToString()))
sb.Append(" AND ");
sb.Append("HAKUNIMI Like '%' + @NameSearch + '%'");
command.Parameters.AddWithValue("NameSearch", NameSearch.Text);
}
if (!string.IsNullOrEmpty(GroupSearch.Text))
{
if (!string.IsNullOrEmpty(sb.ToString()))
sb.Append(" AND ");
sb.Append("KONSERNI Like '%' + @GroupSearch + '%'");
command.Parameters.AddWithValue("GroupSearch", GroupSearch.Text);
}
if (!string.IsNullOrEmpty(IDSearch.Text))
{
if (!string.IsNullOrEmpty(sb.ToString()))
sb.Append(" AND ");
sb.Append("YRNRO Like '%' + @IDSearch + '%'");
command.Parameters.AddWithValue("IDSearch", IDSearch.Text);
}
//command.CommandText = sb.ToString();
_conditions["name"] = sb.ToString();
UpdateFilter();
}
SqlCommand command = new SqlCommand();
StringBuilder sb = new StringBuilder();
...
if (!string.IsNullOrEmpty(NameSearch.Text))
{
if (!string.IsNullOrEmpty(qry))
sb.Append(" AND ");
sb.Append("HAKUNIMI Like '%' + @NameSearch + '%'");
command.Parameters.AddWithValue("NameSearch", NameSearch.Text);
}
...
command.CommandText = sb.ToString():