Strange behavior when searching db for a string that contains quotes
I have a method that searches a database. Part of the search allows users to search by an EXACT string, using quotes. There is a particular string that I'm having trouble with; it's called String "xyz"
. Notice the quotes around xyz. If the user puts quotes around the entire string so it looks like "String "xyz""
, the database row is not returned. It does return properly when the user simply searches for String "xyz"
.
Here's the portion of code in question:
bool isExactSearchString = false;
if (searchString.StartsWith("\"") && searchString.EndsWith("\""))
{
// remove first and last quotes
searchString = searchString.Remove(0, 1);
searchString = searchString.Remove(searchString开发者_高级运维.Length - 1, 1);
isExactSearchString = true;
}
var result = from d in repository.GetAllTheTableRows()
where (isExactSearchString ? d.Column == searchString : d.Column.Contains(searchString))
select d.Driver;
So, result
returns no results when the string being search is surrounded by quotes when there are quotes in the string, but will return results when there are quotes around a string that DOESN'T contain quotes. This seems very odd to me. Anyone know why this is?
if (searchString.StartsWith("\"") && searchString.EndsWith("\""))
{
// remove first and last quotes
searchString = searchString.Remove(0, 1);
searchString = searchString.Remove(searchString.Length - 1, 1);
//
// Replace all occurrences of " in searchString with \"
//
isExactSearchString = true;
}
精彩评论