I would like to know if there is a function to correctly escape string literals for filter expressions. e.g.:
DataTable.Select(String.Format("[name] = \'
///
/// If a pattern in a LIKE clause contains any of these special characters * % [ ], those characters must be escaped in brackets [ ] like this [*], [%], [[] or []].
/// If the pattern is not in a like clause then you can pass valueIsForLIKEcomparison = false to not escape brackets.
/// Examples:
/// - strFilter = "[Something] LIKE '%" + DataTableHelper.EscapeLikeValue(filterValue) + "%'";
///
/// http://www.csharp-examples.net/dataview-rowfilter/
///
/// LIKE filterValue. This should not be the entire filter string... just the part that is being compared.
/// Whether or not the filterValue is being used in a LIKE comparison.
///
public static string EscapeFilterValue(string filterValue, bool valueIsForLIKEcomparison = true)
{
string lb = "~~LeftBracket~~";
string rb = "~~RightBracket~~";
filterValue = filterValue.Replace("[", lb).Replace("]", rb).Replace("*", "[*]").Replace("%", "[%]").Replace("'", "''");
if (valueIsForLIKEcomparison)
{
filterValue = filterValue.Replace(lb, "[[]").Replace(rb, "[]]");
}
else
{
filterValue = filterValue.Replace(lb, "[").Replace(rb, "]");
}
return filterValue;
}