C# 如何在使用BindingSource';s过滤器特性

C# 如何在使用BindingSource';s过滤器特性,c#,.net,winforms,C#,.net,Winforms,我继承了一个C#应用程序,它通过DataGridView列出数据库表中的数据。还有一些文本字段用于过滤这些数据。这是这样做的: String listFilter = string.Format("City = '{0}'", this.toolStripSearch.Text); this.customersBindingSource.Filter = listFilter; toolStripSearch是按城市名称搜索的文本字段。问题是没有SQL转义。向字段添加报价会导致应用程序崩溃 什

我继承了一个C#应用程序,它通过DataGridView列出数据库表中的数据。还有一些文本字段用于过滤这些数据。这是这样做的:

String listFilter = string.Format("City = '{0}'", this.toolStripSearch.Text);
this.customersBindingSource.Filter = listFilter;
toolStripSearch是按城市名称搜索的文本字段。问题是没有SQL转义。向字段添加报价会导致应用程序崩溃


什么是这里转义SQL的正确方法?

我认为
BindingSource.Filter
的“正确”方法是简单地使用
'
转义单引号,听起来很可怕。例如,类似于

String listFilter = string.Format("City = '{0}'", this.toolStripSearch.Text.Replace("'", "''")); 
this.customersBindingSource.Filter = listFilter;  
BindingSource
绑定到数据库中的数据时,
Filter
属性使用与
DataColumn
表达式相同的语法

您可以在此处找到有关所需语法的文档:

从该链接:

字符串值应包含在内 在单引号内(和 表中的每个单引号字符 字符串值必须由 准备另一首单曲 引号字符)


事实上,我想他只是想。。。此.toolStripSearch.Text.Replace(“,”)(其中有单引号)。。。。正确的?因为他只需要转义筛选文本,而不需要转义整个筛选语句。@Tony-是的,你是对的-我原来的代码是错的。现在已经纠正了。