<?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>#Adam.Blog# &#187; C++</title>
	<atom:link href="http://blog.adampresley.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.adampresley.com</link>
	<description>I&#039;m bringin&#039; nerdy back.</description>
	<lastBuildDate>Fri, 03 Sep 2010 15:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><atom:link rel="hub" href="http://github.com/tonyg/rabbithub"/>		<item>
		<title>Experimenting with SQL to JSON in SQL Server 2008</title>
		<link>http://blog.adampresley.com/2010/experimenting-with-sql-to-json-in-sql-server-2008/</link>
		<comments>http://blog.adampresley.com/2010/experimenting-with-sql-to-json-in-sql-server-2008/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 14:37:08 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=981</guid>
		<description><![CDATA[In the last couple of days at work my friend Adrian (@iknowkungfoo) and I (@adampresley) have been tossing around how we can improve performance on various portions of the application we work on. On of the trouble areas has always been large query sets that then have to be transformed into JSON for an AJAX [...]


Related posts:<ol><li><a href='http://blog.adampresley.com/2009/writing-csharp-functions-for-sql-server-and-clr-integration/' rel='bookmark' title='Permanent Link: Writing C# Functions for SQL Server and CLR Integration'>Writing C# Functions for SQL Server and CLR Integration</a></li>
<li><a href='http://blog.adampresley.com/2007/images-in-sql/' rel='bookmark' title='Permanent Link: Images in SQL'>Images in SQL</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In the last couple of days at work my friend Adrian (@iknowkungfoo) and I (@adampresley) have been tossing around how we can improve performance on various portions of the application we work on. On of the trouble areas has always been large query sets that then have to be transformed into JSON for an AJAX response.</p>

<p>One little feature introduced in SQL Server 2005 that I had completely forgot about is the ability to take a given query result set and turn it into XML. It&#8217;s a very nifty feature, and once Adrian reminded me of this some thoughts started to take shape. First let&#8217;s look at how the XML feature works. Below is a query that I ran against a test database that contains address information. Below that is a screenshot of how the result set looks using the XML feature.</p>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
    id
    <span style="color: #66cc66;">,</span> firstName
    <span style="color: #66cc66;">,</span> lastName
    <span style="color: #66cc66;">,</span> email
    <span style="color: #66cc66;">,</span> phone
    <span style="color: #66cc66;">,</span> postalCode
<span style="color: #993333; font-weight: bold;">FROM</span> address
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
    firstName<span style="color: #66cc66;">,</span> lastName 
<span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'addresses'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p><a href="http://blog.adampresley.com/wp-content/uploads/2010/07/ScreenHunter_01-2010-07-14.gif"><img src="http://blog.adampresley.com/wp-content/uploads/2010/07/ScreenHunter_01-2010-07-14.gif" alt="" title="SQL As XML" width="502" height="137" class="alignnone size-full wp-image-983" /></a></p>

<p><span id="more-981"></span></p>

<p>Ok, so that&#8217;s pretty cool. So what I&#8217;ve done so far is write a .NET assembly that I register in SQL Server, wrap a SQL function around the .NET method, and am using this to turn the XML from the query into JSON. For more info on creating and registering a .NET method in SQL server see my previous post <a href="http://blog.adampresley.com/2009/writing-csharp-functions-for-sql-server-and-clr-integration/">Writing C# Functions for SQL Server and CLR Integration</a>. So without any ado here is the code for the assembly.</p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.SqlTypes</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.SqlClient</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Server</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml.XPath</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/**
 * The JSONTransformer class provides methods to encode XML datasets
 * into JSON. These methods are intended to be used by a 
 * Microsoft SQL Server 2005 or higher.
 * @author Adam Presley
 */</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> JSONTransformer
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/**
     * This method takes an XML dataset and serializes it to a 
     * JSON string acceptable for AJAX/client use.
     * 
     * &lt;code&gt;
     * DECLARE @resultSetXML VARCHAR(max);
     * DECLARE @rootNodeName VARCHAR(75);
     * DECLARE @elementNodeName VARCHAR(75);
     * DECLARE @result VARCHAR(max);
     * 
     * SET @rootNodeName = 'addresses';
     * SET @elementNodeName = 'address';
     * 
     * SET @resultSetXML = (
     *     SELECT
     *         id
     *         , firstName
     *         , lastName
     *         , email
     *         , phone
     *         , postalCode
     * FROM address
     *     ORDER BY
     *         firstName, lastName 
     * FOR XML AUTO, ROOT('addresses'));
     *     
     * SELECT dbo.SQLJsonEncode(@resultSetXML, @rootNodeName, @elementNodeName) AS jsonResult;
     * &lt;/code&gt;
     * 
     * @author Adam Presley
     * @param resultSetXML - The SQL result set serialized to XML
     * @param rootNodeName - The name of the XML root node
     * @param elementNodeName - The name of a record's XML node
     * @returns A string containing the resultset as JSON.
     */</span>
    <span style="color: #000000;">&#91;</span>SqlFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Encode<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> resultSetXML, <span style="color: #FF0000;">string</span> rootNodeName, <span style="color: #FF0000;">string</span> elementNodeName<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        XmlDocument xmlDoc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        DataSet dataset <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataSet<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        StringBuilder result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> recordCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> currentIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/*
         * Create an XPath iterator.
         */</span>
        XmlTextReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlTextReader<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> StringReader<span style="color: #000000;">&#40;</span>resultSetXML<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        XPathDocument xdoc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XPathDocument<span style="color: #000000;">&#40;</span>reader<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        XPathNavigator nav <span style="color: #008000;">=</span> xdoc.<span style="color: #0000FF;">CreateNavigator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        XPathNodeIterator iter <span style="color: #008000;">=</span> nav.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>rootNodeName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;/&quot;</span> <span style="color: #008000;">+</span> elementNodeName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        recordCount <span style="color: #008000;">=</span> iter.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
        currentIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{ <span style="color: #008080; font-weight: bold;">\&quot;</span>recordcount<span style="color: #008080; font-weight: bold;">\&quot;</span>: <span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> recordCount.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>, <span style="color: #008080; font-weight: bold;">\&quot;</span>data<span style="color: #008080; font-weight: bold;">\&quot;</span>: [ &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>iter.<span style="color: #0000FF;">MoveNext</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            XPathNavigator item <span style="color: #008000;">=</span> iter.<span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span>
            result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{ &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">/*
             * If we have attributes iterate over them and add them to
             * the JSON object.
             */</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">HasAttributes</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                item.<span style="color: #0000FF;">MoveToFirstAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">string</span> line <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">do</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #FF0000;">string</span> name <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
                    <span style="color: #FF0000;">string</span> value <span style="color: #008000;">=</span> item.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
                    line <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> name <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>: <span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;">+</span> __jsonEscape<span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>, &quot;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">MoveToNextAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">/*
                 * Remove trailing comma
                 */</span>
                line <span style="color: #008000;">=</span> line.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, line.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; }&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>currentIndex <span style="color: #008000;">&lt;</span> recordCount <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;, &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            currentIndex<span style="color: #008000;">++;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; ]}&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        reader.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> result.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/**
     * This method escapes a string in a format suitable
     * for JSON and JavaScript.
     * @author Adam Presley
     * @param data - The piece of data to JavaScript encode
     * @returns A string properly encoded for JavaScript
     */</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> __jsonEscape<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> data<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> result <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
&nbsp;
        result <span style="color: #008000;">=</span> result.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span><span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<p>Now we can run a query or proc something like this to see JSON data.</p>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">DECLARE @resultSetXML VARCHAR<span style="color: #66cc66;">&#40;</span>max<span style="color: #66cc66;">&#41;</span>;
DECLARE @rootNodeName VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">75</span><span style="color: #66cc66;">&#41;</span>;
DECLARE @elementNodeName VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">75</span><span style="color: #66cc66;">&#41;</span>;
DECLARE @result VARCHAR<span style="color: #66cc66;">&#40;</span>max<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @rootNodeName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'addresses'</span>;
<span style="color: #993333; font-weight: bold;">SET</span> @elementNodeName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'address'</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @resultSetXML <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span>
		id
		<span style="color: #66cc66;">,</span> firstName
		<span style="color: #66cc66;">,</span> lastName
		<span style="color: #66cc66;">,</span> email
		<span style="color: #66cc66;">,</span> phone
		<span style="color: #66cc66;">,</span> postalCode
	<span style="color: #993333; font-weight: bold;">FROM</span> address
	<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
		firstName<span style="color: #66cc66;">,</span> lastName 
	<span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'addresses'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>SQLJsonEncode<span style="color: #66cc66;">&#40;</span>@resultSetXML<span style="color: #66cc66;">,</span> @rootNodeName<span style="color: #66cc66;">,</span> @elementNodeName<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> jsonResult;</pre></div></div>


<p><a href="http://blog.adampresley.com/wp-content/uploads/2010/07/ScreenHunter_02-2010-07-14.gif"><img src="http://blog.adampresley.com/wp-content/uploads/2010/07/ScreenHunter_02-2010-07-14.gif" alt="" title="SQL XML to JSON" width="523" height="96" class="alignnone size-full wp-image-984" /></a></p>

<p>Pretty cool! Happy coding!</p>


<p>Related posts:<ol><li><a href='http://blog.adampresley.com/2009/writing-csharp-functions-for-sql-server-and-clr-integration/' rel='bookmark' title='Permanent Link: Writing C# Functions for SQL Server and CLR Integration'>Writing C# Functions for SQL Server and CLR Integration</a></li>
<li><a href='http://blog.adampresley.com/2007/images-in-sql/' rel='bookmark' title='Permanent Link: Images in SQL'>Images in SQL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2010/experimenting-with-sql-to-json-in-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>And the lightbulb lit up&#8230;</title>
		<link>http://blog.adampresley.com/2008/and-the-lightbulb-lit-up/</link>
		<comments>http://blog.adampresley.com/2008/and-the-lightbulb-lit-up/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 06:36:48 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/2008/03/07/and-the-lightbulb-lit-up/</guid>
		<description><![CDATA[In my last post I mentioned I was having issue with remembering the order to draw vertices for quads. Ha&#8230; the light came on tonight while I was working on the various tutorials I&#8217;ve come across. The bottom and back faces of a cube I am actually drawing the quad from the perspective of viewing [...]


Related posts:<ol><li><a href='http://blog.adampresley.com/2008/learning-opengl/' rel='bookmark' title='Permanent Link: Learning OpenGL'>Learning OpenGL</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In my last post I mentioned I was having issue with remembering the order to draw vertices for quads. Ha&#8230; the light came on tonight while I was working on the various tutorials I&#8217;ve come across. The bottom and back faces of a cube I am actually drawing the quad from the perspective of viewing the back face. Apparently when drawing a quad (or triangle for that matter) you draw the vertices in counter-clockwise order to draw the <strong>FRONT</strong> face as the side facing the observer, or you draw the vertices clockwise to draw the <strong>BACK</strong> face as the side facing the observer. Boy, once I got that, it sure seemed a lot easier!</p>

<p>As an example of drawing a basic cube, colored all pretty and green.</p>

<p><img src="http://blog.adampresley.com/wp-content/uploads/2008/03/openglblocksscreenie.jpg" alt="OpenGL Blocks Screenie" /></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">glBegin<span style="color: #008000;">&#40;</span>GL_QUADS<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Front face</span>
glNormal3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glColor3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Back face</span>
glNormal3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">0.0f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">1.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glColor3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Top face</span>
glNormal3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glColor3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Bottom face</span>
glNormal3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glColor3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Right face</span>
glNormal3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glColor3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Left face</span>
glNormal3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glColor3f<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0f</span>, <span style="color:#800080;">1.0f</span>, <span style="color:#800080;">0.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
glVertex3f<span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color:#800080;">1.2f</span>, <span style="color:#800080;">0.5f</span>, <span style="color: #000040;">-</span><span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
glEnd<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>



<p>Related posts:<ol><li><a href='http://blog.adampresley.com/2008/learning-opengl/' rel='bookmark' title='Permanent Link: Learning OpenGL'>Learning OpenGL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2008/and-the-lightbulb-lit-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning OpenGL</title>
		<link>http://blog.adampresley.com/2008/learning-opengl/</link>
		<comments>http://blog.adampresley.com/2008/learning-opengl/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 15:38:57 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/2008/03/06/learning-opengl/</guid>
		<description><![CDATA[In the last two days I have blown the dust film off of the old C++ compiler and jumped back into the world of graphic programming in OpenGL. I have maintained a fantasy for many years now of getting into game programming. Being such a fascinating and challenging area of development, as well as the [...]


Related posts:<ol><li><a href='http://blog.adampresley.com/2008/and-the-lightbulb-lit-up/' rel='bookmark' title='Permanent Link: And the lightbulb lit up&#8230;'>And the lightbulb lit up&#8230;</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In the last two days I have blown the dust film off of the old C++ compiler and jumped back into the world of graphic programming in OpenGL. I have maintained a fantasy for many years now of getting into game programming. Being such a fascinating and challenging area of development, as well as the fact that I enjoy video games, has prompted me to start looking into higher education on the matter.</p>

<p>I am not a college graduate and I have worked hard to get into the software development arena, but now I find that I must seek higher education to get into the arena of game development. I have looked into a couple of schools, mainly <a href="http://guildhall.smu.edu/" target="_blank">SMU&#8217;s GuildHall</a> and <a href="http://www.gamedegree.com/" target="_blank">UAT&#8217;s Bachelor of Science: Game Programming</a> degree programs. There are a couple of others I&#8217;ve seen, so I haven&#8217;t made a choice yet, as I still need to investigate further, not to mention calling all these people. Meanwhile, to get started, I am refreshing my C++ skills, and jumping onto every OpenGL tutorial I can find.</p>

<p><span id="more-143"></span>Right now I am getting my head wrapped around correctly mapping vertex points in the 3D space, my biggest confusion being what corner vertex to start with based on the direction the front face points, and then which direction to continue adding vertices (counter-clockwise I am told). Once I get that straight I should have no issue with texture mapping coordinates, as I understand their origin and directions, and how to map them to a vertex.</p>

<p>So basically the coordinate system works with the center screen as origin. The X axis runs to the left and right, with the left side of the origin going into the negative numbers, and the right side running positive. The Y axis runs up and down, with the up direction being positive, and the down direction being negative. The Z axis runs into and out of the screen. The further into the direction of the screen you go the lower the number (negative numbers), and the further out you come, toward the viewer (me), the higher the number (positive).</p>

<p>Now I am learning about texture mapping, and the first thing I had to overcome there was the fact that a lot of tutorials I was looking at were using GLAUX for loading images for textures. It appears that support for GLAUX has been dropped, so I had to spend a little time looking for either a good reference on loading various image formats, or find a nice library that does that for me. Well, I settled on <a href="http://openil.sourceforge.net/" target="_blank">DevIL</a> which is a full featured image loading and manipulation library written in C++. Quite featured and offers native OpenGL support for loading textures. Once that was solved it was a simple matter of learning to map a corner of a texture to a vertex on a quad. I&#8217;m still a little fuzzy on some of the details, but it will come with practice.</p>

<p>The part that I dread the most? All of the complex math. I&#8217;ve never been a good math student, and here I will have to learn oodles of it! Ah well, that is the sacrifice I must make to live the dream baby!</p>


<p>Related posts:<ol><li><a href='http://blog.adampresley.com/2008/and-the-lightbulb-lit-up/' rel='bookmark' title='Permanent Link: And the lightbulb lit up&#8230;'>And the lightbulb lit up&#8230;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2008/learning-opengl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Images in SQL</title>
		<link>http://blog.adampresley.com/2007/images-in-sql/</link>
		<comments>http://blog.adampresley.com/2007/images-in-sql/#comments</comments>
		<pubDate>Fri, 09 Feb 2007 23:21:24 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=58</guid>
		<description><![CDATA[So I was presented with a task today of extracting images that were stored in a Microsoft SQL Server 2000 database and save them as JPEG files. Here is a bit of code that will do this. In this code sample I am assuming domain authenticated SQL login. The table name here will be called [...]


Related posts:<ol><li><a href='http://blog.adampresley.com/2010/experimenting-with-sql-to-json-in-sql-server-2008/' rel='bookmark' title='Permanent Link: Experimenting with SQL to JSON in SQL Server 2008'>Experimenting with SQL to JSON in SQL Server 2008</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>So I was presented with a task today of extracting images that were stored in a Microsoft SQL Server 2000 database and save them as JPEG files. Here is a bit of code that will do this. In this code sample I am assuming domain authenticated SQL login. The table name here will be called <strong>Photos</strong> and the column names <strong>id</strong>, <strong>type</strong>, and <strong>photo</strong>.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SqlConnection connection<span style="color: #008000;">;</span>
SqlCommand command<span style="color: #008000;">;</span>
SqlDataReader reader<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span> outputDirectory <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;C:<span style="color: #008080; font-weight: bold;">\\</span>TestLocation&quot;</span><span style="color: #008000;">;</span>
PictureBox pic <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PictureBox<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>connection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;data source=MyServer;initial catalog=Database;persist security info=True;packet size=4096;Trusted_Connection=True&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   connection.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   command <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlCommand<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;SELECT id, type, photo FROM Photos&quot;</span>, connection<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   command.<span style="color: #0000FF;">CommandType</span> <span style="color: #008000;">=</span> CommandType.<span style="color: #0000FF;">Text</span><span style="color: #008000;">;</span>
   command.<span style="color: #0000FF;">CommandTimeout</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">3000</span><span style="color: #008000;">;</span>
&nbsp;
   reader <span style="color: #008000;">=</span> command.<span style="color: #0000FF;">ExecuteReader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>reader.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">try</span>
      <span style="color: #000000;">&#123;</span>
         <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> rawBytes <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> reader<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;photo&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
         MemoryStream memStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         memStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>rawBytes, <span style="color: #FF0000;">0</span>, rawBytes.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
         <span style="color: #FF0000;">string</span> filename <span style="color: #008000;">=</span> outputDirectory <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>image-&quot;</span> <span style="color: #008000;">+</span> reader<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;id&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;-&quot;</span> <span style="color: #008000;">+</span> reader<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;type&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.jpg&quot;</span><span style="color: #008000;">;</span>
&nbsp;
         pic.<span style="color: #0000FF;">Image</span> <span style="color: #008000;">=</span> Image.<span style="color: #0000FF;">FromStream</span><span style="color: #000000;">&#40;</span>memStream<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
         pic.<span style="color: #0000FF;">Image</span>.<span style="color: #0000FF;">Save</span><span style="color: #000000;">&#40;</span>filename, <span style="color: #000000;">System.<span style="color: #0000FF;">Drawing</span>.<span style="color: #0000FF;">Imaging</span></span>.<span style="color: #0000FF;">ImageFormat</span>.<span style="color: #0000FF;">Jpeg</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
         memStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
      <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>


<p>This C# code will loop over a record set and save the images stored in the table as JPEG files. Happy coding!</p>


<p>Related posts:<ol><li><a href='http://blog.adampresley.com/2010/experimenting-with-sql-to-json-in-sql-server-2008/' rel='bookmark' title='Permanent Link: Experimenting with SQL to JSON in SQL Server 2008'>Experimenting with SQL to JSON in SQL Server 2008</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2007/images-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logical Drives and WMI</title>
		<link>http://blog.adampresley.com/2007/logical-drives-and-wmi/</link>
		<comments>http://blog.adampresley.com/2007/logical-drives-and-wmi/#comments</comments>
		<pubDate>Thu, 01 Feb 2007 19:39:37 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=57</guid>
		<description><![CDATA[In an effort to support removable media devices in SyncXpress I&#8217;ve had to do a bit of learning about WMI in .NET. It is actually quite cool how much information you can get from WMI about ANY piece of hardware attached to your system, and even INSIDE your system. So in my case I want [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>In an effort to support removable media devices in SyncXpress I&#8217;ve had to do a bit of learning about WMI in .NET. It is actually quite cool how much information you can get from WMI about ANY piece of hardware attached to your system, and even INSIDE your system. So in my case I want to find out information about a logical drive device in Windows. Using the <em>System.Management</em> namespace we can access the <em>ManagementObject</em> classes that will allow us to query the WMI services to get information about our device. Here is an example of getting information about your <strong>C:\</strong> drive.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">ManagementObject wmi<span style="color: #008000;">;</span>
<span style="color: #FF0000;">string</span> serialNumber, name, freeSpace<span style="color: #008000;">;</span>
&nbsp;
wmi <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManagementObject<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Win32_LogicalDisk.DeviceID=<span style="color: #008080; font-weight: bold;">\&quot;</span>C:<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
serialNumber <span style="color: #008000;">=</span> wmi<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;VolumeSerialNumber&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
name <span style="color: #008000;">=</span> wmi<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
freeSpace <span style="color: #008000;">=</span> wmi<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FreeSpace&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>


<p>For more information on what data can be queried on a logical device see <a href="http://msdn2.microsoft.com/en-us/library/aa394173.aspx" target="_blank">http://msdn2.microsoft.com/en-us/library/aa394173.aspx</a></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2007/logical-drives-and-wmi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
