<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AkillesBlog &#187; escapecharacters</title>
	<atom:link href="http://blog.akilles.org/tag/escapecharacters/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.akilles.org</link>
	<description>Talk on programming, computers, electronics, web etc</description>
	<lastBuildDate>Sun, 13 Jun 2010 02:42:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Escaping backslash character in C# .NET MySQL-queries</title>
		<link>http://blog.akilles.org/2008/03/12/escaping-backslash-character-in-c-net-mysql-queries/</link>
		<comments>http://blog.akilles.org/2008/03/12/escaping-backslash-character-in-c-net-mysql-queries/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 00:20:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[escapecharacters]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.akilles.org/2008/03/12/escaping-backslash-character-in-c-net-mysql-queries/</guid>
		<description><![CDATA[Using escape-sequences like \t or \n is one thing, but escaping the escapecharacter (often &#8216;\&#8217;) can sometimes be a pain&#8230; This is especially true when the string you&#8217;re escaping is about to be processed by several succeeding processes, like: 1) a compiler, 2) a regex-engine, 3) a database, &#8230; , etc.
Anyways, I&#8217;ve experimented some with [...]]]></description>
			<content:encoded><![CDATA[<p>Using escape-sequences like \t or \n is one thing, but escaping the escapecharacter (often &#8216;\&#8217;) can sometimes be a pain&#8230; This is especially true when the string you&#8217;re escaping is about to be processed by several succeeding processes, like: 1) a compiler, 2) a regex-engine, 3) a database, &#8230; , etc.</p>
<p>Anyways, I&#8217;ve experimented some with MySql-queries from within C# .NET, using Windows paths (containing backslashes as path separators) in the WHERE-clauses. We have a query of one of the forms:</p>
<p><strong>case 1: = operator</strong></p>
<pre name="code" class="c#">
querystring1 =
  &quot;SELECT a,b,path FROM table &quot; +
  &quot;WHERE path = '&quot; + patharg1 + &quot;';&quot;;
</pre>
<p><em>or</em></p>
<p><strong>case 2: LIKE operator</strong></p>
<pre name="code" class="c#">
querystring2 =
  &quot;SELECT a,b,path FROM table &quot; +
  &quot;WHERE path LIKE '&quot; + patharg2 + &quot;';&quot;;
</pre>
<p>Here is what I&#8217;ve found out:</p>
<p><strong>case 1: = operator</strong><br />
Use <strong>4</strong> backslashes for each backslash in the saved record you&#8217;re matching against. You could do:</p>
<pre name="code" class="c#">
patharg1 =
  patharg1.Replace(&quot;\\&quot;, &quot;\\\\&quot;);
</pre>
<p>(The 2 backslashes in the first replace-argument is for escaping the backslash within the compiler.)</p>
<p><strong>case 1: LIKE operator</strong><br />
Use <strong>8</strong>(!) backslashes for each backslash in the saved record you&#8217;re matching against. You could do:</p>
<pre name="code" class="c#">
patharg2 =
  patharg2.Replace(&quot;\\&quot;, &quot;\\\\\\\\&quot;);
</pre>
<p>(The 2 backslashes in the first replace-argument is for escaping the backslash within the compiler.)</p>
<p>The two cases could then look like this:</p>
<p><strong>case 1:</strong></p>
<pre name="code" class="c#">
querystring1 =
  &quot;SELECT a,b,path FROM table &quot; +
  &quot;WHERE path = 'c:\\\\some\\\\path.txt';&quot;;
</pre>
<p><strong>case 2:</strong></p>
<pre name="code" class="c#">
querystring2 =
  &quot;SELECT a,b,path FROM table &quot; +
  &quot;WHERE path LIKE 'c:\\\\\\\\some\\\\\\\\path.txt';&quot;;
</pre>
<p>The above is if you put the querystring into, for instance, a MySqlCommand&#8217;s CommandText.</p>
<p>However, if you use datasets and run something like</p>
<pre name="code" class="c#">
dataTable.Select(filterExpression)
</pre>
<p>where dataTable is a DataTable instance and filterExpression is a string, you must <strong>not</strong> escape the backslash more than for a usual string (you&#8217;ll just write &#8220;\\&#8221; or @&#8221;\&#8221;, because some kind of escape of the backslash is <em>always</em> necessary for the compiler).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.akilles.org/2008/03/12/escaping-backslash-character-in-c-net-mysql-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
