<?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>DucDigital &#187; sql</title>
	<atom:link href="http://www.ducdigital.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ducdigital.com</link>
	<description>for ( $girl = 1; $girl &#60; $required; $girl++ ) { echo "I love DucDigital"; }</description>
	<lastBuildDate>Sat, 07 Aug 2010 09:12:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Cache individual object in asp.net mvc</title>
		<link>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/</link>
		<comments>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 05:37:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Asp.net MVC]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[httpcontext]]></category>
		<category><![CDATA[linq to sql]]></category>
		<category><![CDATA[linq2sql]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[outputcache]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sap.net mvc]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=218</guid>
		<description><![CDATA[Upon writing a controller for exporting image on the fly, I struggled with the problem that: "Output cache does not cache HttpHeader (in this case, "Location" header)". Which lead me to despair since each time i called the controller to output image, it will have to connect to database and do a couple of query, [...]]]></description>
			<content:encoded><![CDATA[<p>Upon writing a controller for exporting image on the fly, I struggled with the problem that: "Output cache does not cache HttpHeader (in this case, "Location" header)". Which lead me to despair since each time i called the controller to output image, it will have to connect to database and do a couple of query, which is not practical in real life. So finally, i found out that i can easily cache objects into a cache provider in Asp.net Mvc. Very useful, all my queries go back to 0 (traced through Ling2sql profiler). </p>
<p>You can use this piece of code in anywhere of your code where you want to cache any object.<br />
(Notice: this code was used in a controller, so I used HttpContext.Cache. But if you use else where that make this code not working, try HttpContext.Current.Cache)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ObjName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                link <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>HttpContext.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ObjName&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> link<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                HttpContext.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ObjName&quot;</span>, link, <span style="color: #0600FF;">null</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">NoAbsoluteExpiration</span>, <span style="color: #008000;">new</span> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">30</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">CacheItemPriority</span>.<span style="color: #0000FF;">High</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> link<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>What it did was to check if the ObjName exist in the context, if not, create one and dump the data into it. Remember when you call back from HttpContext, it's an object so you always need a cast. If you don't know about argument, let the intellisense do the work for you.</p>
<p>Time to exand further about the topic: <a href="http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add(VS.80).aspxx">HttpContext.Cache.Add</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2010%2F01%2F14%2Fcache-individual-object-in-asp-net-mvc%2F&amp;linkname=Cache%20individual%20object%20in%20asp.net%20mvc"><img src="http://www.ducdigital.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NVARCHAR(MAX) now&#8230;</title>
		<link>http://www.ducdigital.com/2009/12/22/nvarcharmax-now/</link>
		<comments>http://www.ducdigital.com/2009/12/22/nvarcharmax-now/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 13:19:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[mssql2008]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlserver]]></category>
		<category><![CDATA[Structured Query Language]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=188</guid>
		<description><![CDATA[Recently, I need to write a Trigger to duplicate and save a copy of revision automatically into the database, and at that time, i encoutered an error: Cannot use text, ntext, or image columns in the ‘inserted’ and ‘deleted’ tables. This cause me so much trouble since what else should i use if I am [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I need to write a Trigger to duplicate and save a copy of revision automatically into the database, and at that time, i encoutered an error:</p>
<blockquote><p>Cannot use text, ntext, or image columns in the ‘inserted’ and ‘deleted’ tables.</p></blockquote>
<p>This cause me so much trouble since what else should i use if I am not allowed to use text/ntext? This make me wandering around google for a while, and finally there is a solution...</p>
<p>The NTEXT, TEXT, IMAGE is deprecated since 2005 and will be remove in the future release, maybe 2008R2 since MSSQL2008 still work fine with NTEXT, TEXT</p>
<ul>
<li>NTEXT -&gt; replaced by NVARCHAR(MAX)</li>
<li>TEXT -&gt; replaced by VARCHAR(MAX)</li>
<li>IMAGE -&gt; replaced by VARBINARY(MAX)</li>
</ul>
<p>And of course, for those who wondering, the maximum limit of VARCHAR is 8000 and NVARCHAR is 4000 (2 bytes for each unicode character) due to SQL Server uses 8KB page to store data to disk. But of course, Varchar(MAX), NVarchar(MAX) and VarBinary(MAX) data types in SQL Server 2005 which allows you to save upto 2GB in a single variable. The best part is that It allows you to use these data types as stored procedure parameters, internal variables etc.</p>
<p>So I suggest you should stop using NTEXT and TEXT and switch to NVARCHAR/VARCHAR soon!</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2009%2F12%2F22%2Fnvarcharmax-now%2F&amp;linkname=NVARCHAR%28MAX%29%20now%26%238230%3B"><img src="http://www.ducdigital.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.ducdigital.com/2009/12/22/nvarcharmax-now/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert INT to VARCHAR in SQL (MSSQL)</title>
		<link>http://www.ducdigital.com/2009/12/20/convert-int-to-varchar-in-sql-mssql/</link>
		<comments>http://www.ducdigital.com/2009/12/20/convert-int-to-varchar-in-sql-mssql/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 13:10:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[mssql2008]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlserver]]></category>
		<category><![CDATA[Structured Query Language]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=186</guid>
		<description><![CDATA[At some point you will need to change INT to VARCHAR for the purpose of manipulation like substring or to display it in the status windows when execute of stored procedure or trigger... here is the code which you can use to convert / cast the INT to VARCHAR: CONVERT&#40;TYPE, VALUE&#41; in this case ( [...]]]></description>
			<content:encoded><![CDATA[<p>At some point you will need to change INT to VARCHAR for the purpose of manipulation like substring or to display it in the status windows when execute of stored procedure or trigger...</p>
<p>here is the code which you can use to convert / cast the INT to VARCHAR:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">CONVERT<span style="color: #66cc66;">&#40;</span>TYPE<span style="color: #66cc66;">,</span> VALUE<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>in this case ( my case ), this was used:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">	PRINT <span style="color: #ff0000;">'New revision updated for post id '</span> <span style="color: #66cc66;">+</span> CONVERT<span style="color: #66cc66;">&#40;</span>VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @intPID<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'!'</span></pre></div></div>

<p>Good luck</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2009%2F12%2F20%2Fconvert-int-to-varchar-in-sql-mssql%2F&amp;linkname=Convert%20INT%20to%20VARCHAR%20in%20SQL%20%28MSSQL%29"><img src="http://www.ducdigital.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.ducdigital.com/2009/12/20/convert-int-to-varchar-in-sql-mssql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Query a set of IDs from SQL using LINQ to SQL</title>
		<link>http://www.ducdigital.com/2009/12/11/query-a-set-of-ids-from-sql-using-linq-to-sql/</link>
		<comments>http://www.ducdigital.com/2009/12/11/query-a-set-of-ids-from-sql-using-linq-to-sql/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 06:11:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# csharp]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linq to sql]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[where]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=144</guid>
		<description><![CDATA[Just not so long ago, I have a hard time while doing this in LINQ to SQL since i have my datacontext running on LINQ to SQL. I was force to do a N+1 querry, which is extremely expensive on both CPU and Time since i put my SQL Server else where that not in [...]]]></description>
			<content:encoded><![CDATA[<p>Just not so long ago, I have a hard time while doing this in LINQ to SQL since i have my datacontext running on LINQ to SQL. I was force to do a N+1 querry, which is extremely expensive on both CPU and Time since i put my SQL Server else where that not in local and each query cost me 200ms in time.</p>
<p>My N+1 query was look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Image <span style="color: #993333; font-weight: bold;">WHERE</span> IMG_ID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Image <span style="color: #993333; font-weight: bold;">WHERE</span> IMG_ID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Image <span style="color: #993333; font-weight: bold;">WHERE</span> IMG_ID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">7</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Image <span style="color: #993333; font-weight: bold;">WHERE</span> IMG_ID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">9</span></pre></div></div>

<p>however, later i found out a way to solve this by using "<strong>Contains</strong>" from <strong><a href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx">IList </a></strong>to query <strong>"WHERE IN(x,x,x)".</strong></p>
<p>Here is one function in my Repository:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> List<span style="color: #008000;">&lt;</span>Image<span style="color: #008000;">&gt;</span> Images<span style="color: #000000;">&#40;</span>IList<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> idList<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>from d <span style="color: #0600FF;">in</span> _db.<span style="color: #0000FF;">Images</span>
               .<span style="color: #0000FF;">Where</span><span style="color: #008000;">&lt;</span>Image<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> idList.<span style="color: #0000FF;">Contains</span><span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>d.<span style="color: #0000FF;">IMG_ID</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               select d<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #008000;">&lt;</span>Image<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>or better:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #0600FF;">return</span> _db.<span style="color: #0000FF;">Images</span>.<span style="color: #0000FF;">Where</span><span style="color: #008000;">&lt;</span>Image<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> idList.<span style="color: #0000FF;">Contains</span><span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>x.<span style="color: #0000FF;">IMG_ID</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #008000;">&lt;</span>Image<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>these will output:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Image <span style="color: #993333; font-weight: bold;">WHERE</span> IMG_ID <span style="color: #993333; font-weight: bold;">IN</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2009%2F12%2F11%2Fquery-a-set-of-ids-from-sql-using-linq-to-sql%2F&amp;linkname=Query%20a%20set%20of%20IDs%20from%20SQL%20using%20LINQ%20to%20SQL"><img src="http://www.ducdigital.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.ducdigital.com/2009/12/11/query-a-set-of-ids-from-sql-using-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
