Escaping the escape character does not work – SQL LIKE Operator
Я использовал \
как escape character
за LIKE
оператор. I am escaping following four characters
1 %
2 [
3 ]
4 _
When I pass the escape character as input, the query does not return a value. Как я могу заставить это работать?
Data Insert
DECLARE @Text VARCHAR(MAX)
SET @Text = 'Error \\\ \\ C:\toolbox\line 180'
INSERT INTO Account (AccountNumber,AccountType,Duration,ModifiedTime)
VALUES (198,@Text,1,GETDATE())
КОД
static void Main(string[] args)
{
string searchValue1 = @"Error \\\ \\ C:\toolbox\line 180";
string searchValue2 = @"55555";
string result1 = DisplayTest(searchValue1);
string result2 = DisplayTest(searchValue2);
Console.WriteLine("result1:: " + result1);
Console.WriteLine("result2:: " + result2);
Console.ReadLine();
}}
private static string DisplayTest(string searchValue)
{
searchValue = CustomFormat(searchValue);
string test = String.Empty;
string connectionString = "Data Source=.;Initial Catalog=LibraryReservationSystem;Integrated Security=True;Connect Timeout=30";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string commandText = @"SELECT AccountType,*
FROM Account
WHERE AccountType LIKE @input ESCAPE '\'";
using (SqlCommand command = new SqlCommand(commandText, connection))
{
command.CommandType = System.Data.CommandType.Text;
command.Parameters.AddWithValue("@input", "%" + searchValue + "%");
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
test = reader.GetString(0);
}
}
}
}
}
return test;
}
private static string CustomFormat(string input)
{
input = input.Replace(@"%", @"\%");
input = input.Replace(@"[", @"\[");
input = input.Replace(@"]", @"\]");
input = input.Replace(@"_", @"\_");
//input = input.Replace(@"\", @"\\");
return input;
}
ССЫЛКА:
2 ответа
Решение
Изменить ваш CustomFormat
метод как это:
private static string CustomFormat(string input)
{
input = input.Replace(@"\", @"\\");
input = input.Replace(@"%", @"\%");
input = input.Replace(@"[", @"\[");
input = input.Replace(@"]", @"\]");
input = input.Replace(@"_", @"\_");
return input;
}
C# код для обработки как
public static string WildcardFormatSpecialCharacter(string source)
{
string formattedResult = string.Empty;
if (!String.IsNullOrEmpty(source))
{
//Escape the escape character
formattedResult = source.Replace(DataLayerConstants.EscapeCharacter, DataLayerConstants.EscapeCharacterWithEscape);
//The %
formattedResult = formattedResult.Replace(DataLayerConstants.Percentage, DataLayerConstants.PercentageWithEscape);
//The [
formattedResult = formattedResult.Replace(DataLayerConstants.OpenSqaureBracket, DataLayerConstants.OpenSqaureBracketWithEscape);
//The ]
formattedResult = formattedResult.Replace(DataLayerConstants.CloseSqaureBracket, DataLayerConstants.CloseSqaureBracketWithEscape);
//The _
formattedResult = formattedResult.Replace(DataLayerConstants.Underscore, DataLayerConstants.UnderscoreWithEscape);
}
return formattedResult;
}
public const string EscapeCharacter = @"\";
public const string EscapeCharacterWithEscape = @"\\";
public const string Percentage = "%";
public const string PercentageWithEscape = @"\%";
public const string OpenSqaureBracket = "[";
public const string OpenSqaureBracketWithEscape = @"\[";
public const string CloseSqaureBracket = "]";
public const string CloseSqaureBracketWithEscape = @"\]";
public const string Underscore = "_";
public const string UnderscoreWithEscape = @"\_";
Другие вещи, которые нужно проверить Использование REPLACE в SQL Query для символов перевода строки / возврата каретки