<?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; C#</title>
	<atom:link href="http://blog.akilles.org/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.akilles.org</link>
	<description>Talk on programming, computers, electronics, web etc</description>
	<lastBuildDate>Sun, 23 Jan 2011 23:46:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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 [...]]]></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><br />
querystring1 =<br />
  &quot;SELECT a,b,path FROM table &quot; +<br />
  &quot;WHERE path = &#8216;&quot; + patharg1 + &quot;&#8217;;&quot;;</p>
<p><em>or</em></p>
<p><strong>case 2: LIKE operator</strong><br />
querystring2 =<br />
  &quot;SELECT a,b,path FROM table &quot; +<br />
  &quot;WHERE path LIKE &#8216;&quot; + patharg2 + &quot;&#8217;;&quot;;</p>
<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:<br />
patharg1 =<br />
  patharg1.Replace(&quot;\\&quot;, &quot;\\\\&quot;);<br />
(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:<br />
patharg2 =<br />
  patharg2.Replace(&quot;\\&quot;, &quot;\\\\\\\\&quot;);<br />
(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><br />
querystring1 =<br />
  &quot;SELECT a,b,path FROM table &quot; +<br />
  &quot;WHERE path = &#8216;c:\\\\some\\\\path.txt&#8217;;&quot;;</p>
<p><strong>case 2:</strong><br />
querystring2 =<br />
  &quot;SELECT a,b,path FROM table &quot; +<br />
  &quot;WHERE path LIKE &#8216;c:\\\\\\\\some\\\\\\\\path.txt&#8217;;&quot;;</p>
<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<br />
dataTable.Select(filterExpression)<br />
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>
		<item>
		<title>Note to self: C# and logical shortcircuiting</title>
		<link>http://blog.akilles.org/2008/03/10/note-to-self-c-and-logical-shortcircuiting/</link>
		<comments>http://blog.akilles.org/2008/03/10/note-to-self-c-and-logical-shortcircuiting/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 17:44:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.akilles.org/2008/03/10/note-to-self-c-and-logical-shortcircuiting/</guid>
		<description><![CDATA[C# (.NET) do short-circuit logical expressions; i.e.: 1) (A &#124;&#124; B) ==> If A is true, then only A is evaluated. 2) (C &#38;&#38; D) ==> If C is false, then only C is evaluated. This means that, provided that A is true, expression 1 is true even though B would throw an exception if [...]]]></description>
			<content:encoded><![CDATA[<p>C# (.NET) <strong>do</strong> short-circuit logical expressions; i.e.:</p>
<p>1)</p>
<pre class="brush: csharp; title: ;">(A || B)</pre>
<p>==> If A is true, then only A is evaluated.</p>
<p>2)</p>
<pre class="brush: csharp; title: ;">(C &amp;&amp; D)</pre>
<p>==> If C is false, then only C is evaluated.</p>
<p>This means that, provided that A is true, expression 1 is true even though B would throw an exception if evaluated.</p>
<p>An example use of expression 2:</p>
<pre class="brush: csharp; title: ;">
if ((filename != &quot;&quot;) &amp;&amp; (new FileInfo(filename).Exists))
	//file exists
</pre>
<p>If filename == &#8220;&#8221;, the Exists-statement would throw an exception. However, since the first expression (&#8230;!=&#8230;) is false, the Exists-statement is never evaluated, and the exception doesn&#8217;t occur.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.akilles.org/2008/03/10/note-to-self-c-and-logical-shortcircuiting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

