From my ASP.NET application I want to issue this query to MySQL
SELECT * FROM responses WHERE field_id LIKE '3\_%'
In other words I am looking for records where the second character is the underscore literal character.
The code generated by the model designer looks like this:
public virtual RiskAnalysis.responsesDataTable GetResponseGroupForAnalysis(int raid, string fieldid) {
this.Adapter.SelectCommand = this.CommandCollection[2];
this.Adapter.SelectCommand.Parameters[0].Value = ((int)(raid));
if ((fieldid == null)) {
throw new global::System.ArgumentNullException("fieldid");
}
else {
this.Adapter.SelectCommand.Parameters[1].Value = ((string)(fieldid));
}
RiskAnalysis.responsesDataTable dataTable = new RiskAnalysis.responsesDataTable();
this.Adapter.Fill(dataTable);
return dataTable;
}
If I call this function like so:
string filter_string = @"3\_%";
ResponsesAdapter.GetResponseGroupForAnalysis(10, filter_string);
the MySQL log reports the query to look like this:
SELECT *
FROM responses
WHERE (ra_id = 10) AND (field_id LIKE '3\\_%')
In other words the I know I’m missing something blindingly obvious here, but how do I place MySQL’s escape backslash in the query without C# (un)helpfully escaping it?
OK, it seems (by experiment – happy to hear explanation or documentation link) that in MySQL
(field_id LIKE '3\_%');is equivalent to(field_id LIKE '3\\_%');and both will find records where field_id starts with ‘3_’. Got myself well confused there – thanks Mahmoud and René for trying to understand what I was getting at!