【发布时间】:2017-06-06 05:47:39
【问题描述】:
我有一个包含一些数据的 DataTable,我想用 LIKE 查询进行过滤,所以我编写了如下代码,一切正常,我正在生成查询并将其传递给 @987654323 的 RowFilter 对象@。
if (results.Count() == 2)
{
if (results[0].ToString() == "" && results[1].ToString() != "")
{
searchString = String.Format("[" + searchBy + "] LIKE '%{0}'", results[1].ToString());
}
else if (results[0].ToString() != "" && results[1].ToString() == "")
{
searchString = String.Format("[" + searchBy + "] LIKE '{0}%'", results[0].ToString());
}
else if (results[0].ToString() != "" && results[1].ToString() != "")
{
searchString = String.Format("[" + searchBy + "] LIKE '{0}%{1}'", results[0].ToString(), results[1].ToString());
}
}
if (searchString != "")
{
DataView dv = new DataView(AccountList.Tables[0]);
dv.RowFilter = searchString;
var tab = dv.ToTable();
DataSet data_set = new DataSet();
data_set.Tables.Add(tab);
ProcessDataSetAndLoadData(data_set);
}
在上面的代码段中工作正常,除了最后一个 else if 条件,在那个条件下,我得到这样的 searchString,
[AccountCode] LIKE 'c%l'
当我将它传递给 RowFilter 时,我得到了如下所示的错误,
Like 运算符错误:字符串模式 'c%l' 无效。
从所有其他条件中,我得到了如下所示的 searchString,并且这些条件工作正常。
[AccountCode] LIKE '%c%'
[AccountCode] LIKE 'c%'
[AccountCode] LIKE '%c
我的问题是为什么我只收到这样的字符串的错误 ?. [AccountCode] LIKE 'c%l'
请建议我完成我的任务。
【问题讨论】:
标签: c# datatable dataview rowfilter