<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Andrew Hay - SQL Server</title>
    <link>http://www.a7drew.com/blog/</link>
    <description>Thinking way too long about the subtitle</description>
    <language>en-us</language>
    <copyright>Andrew Hay</copyright>
    <lastBuildDate>Fri, 20 Aug 2010 23:02:26 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>andrewcameronhay@hotmail.com</managingEditor>
    <webMaster>andrewcameronhay@hotmail.com</webMaster>
    <item>
      <trackback:ping>http://www.a7drew.com/blog/Trackback.aspx?guid=fdd5d3da-f623-4962-b321-fa18c0ab3871</trackback:ping>
      <pingback:server>http://www.a7drew.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.a7drew.com/blog/PermaLink,guid,fdd5d3da-f623-4962-b321-fa18c0ab3871.aspx</pingback:target>
      <dc:creator>Andrew Hay</dc:creator>
      <wfw:comment>http://www.a7drew.com/blog/CommentView,guid,fdd5d3da-f623-4962-b321-fa18c0ab3871.aspx</wfw:comment>
      <wfw:commentRss>http://www.a7drew.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=fdd5d3da-f623-4962-b321-fa18c0ab3871</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I was using a clean machine for some Azure development and it just happened to have
the developer edition of SQL Server, as the default instance, installed instead of
SQL Server Express Edition. When I pressed F5 to launch the local developer app fabric,
I received an error message about the local developer storage. By default, the local
developer storage is looking for .\SQLExpress on your machine.
</p>
        <p>
Here’s the error message that popped up in Visual Studio 2010:
</p>
        <p>
          <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Failed to initialize Development Storage service. See output window for more information." border="0" alt="Failed to initialize Development Storage service. See output window for more information." src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/ConfiguringaSQLServerInstancewithAzureSD_DF84/azure01_1.png" width="499" height="208" />
        </p>
        <p>
I looked in the output window and found this message:
</p>
        <blockquote>
          <p>
Windows Azure Tools: Failed to initialize Development Storage service. Unable to start
Development Storage. Failed to start Development Storage: the SQL Server instance
‘.\SQLExpress’ could not be found.   Please configure the SQL Server instance
for Development Storage using the ‘DSInit’ utility in the Windows Azure SDK.
</p>
        </blockquote>
        <p>
I needed to configure the default instance of SQL Server for Azure local development.
So I searched online for “Azure DSInit” and the first hit shows how to invoke the
command.
</p>
        <p>
          <a title="http://msdn.microsoft.com/en-us/library/dd179457.aspx" href="http://msdn.microsoft.com/en-us/library/dd179457.aspx">http://msdn.microsoft.com/en-us/library/dd179457.aspx</a>
        </p>
        <p>
To configure development storage against the default SQL Server instance:
</p>
        <pre class="csharpcode">DSInit /sqlInstance:.</pre>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <p>
The program DSInit.exe is found in the Azure SDK: 
</p>
        <p>
C:\Program Files\Windows Azure SDK\v1.0\bin\devstore\
</p>
        <p>
Once I ran that program to initialize the local development app storage, I was off
and running with my cloud development tasks.
</p>
        <img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=fdd5d3da-f623-4962-b321-fa18c0ab3871" />
      </body>
      <title>Configuring a local SQL Server Instance with Azure</title>
      <guid isPermaLink="false">http://www.a7drew.com/blog/PermaLink,guid,fdd5d3da-f623-4962-b321-fa18c0ab3871.aspx</guid>
      <link>http://www.a7drew.com/blog/2010/08/20/ConfiguringALocalSQLServerInstanceWithAzure.aspx</link>
      <pubDate>Fri, 20 Aug 2010 23:02:26 GMT</pubDate>
      <description>&lt;p&gt;
I was using a clean machine for some Azure development and it just happened to have
the developer edition of SQL Server, as the default instance, installed instead of
SQL Server Express Edition. When I pressed F5 to launch the local developer app fabric,
I received an error message about the local developer storage. By default, the local
developer storage is looking for .\SQLExpress on your machine.
&lt;/p&gt;
&lt;p&gt;
Here’s the error message that popped up in Visual Studio 2010:
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Failed to initialize Development Storage service. See output window for more information." border="0" alt="Failed to initialize Development Storage service. See output window for more information." src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/ConfiguringaSQLServerInstancewithAzureSD_DF84/azure01_1.png" width="499" height="208"&gt; 
&lt;/p&gt;
&lt;p&gt;
I looked in the output window and found this message:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Windows Azure Tools: Failed to initialize Development Storage service. Unable to start
Development Storage. Failed to start Development Storage: the SQL Server instance
‘.\SQLExpress’ could not be found.&amp;nbsp;&amp;nbsp; Please configure the SQL Server instance
for Development Storage using the ‘DSInit’ utility in the Windows Azure SDK.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
I needed to configure the default instance of SQL Server for Azure local development.
So I searched online for “Azure DSInit” and the first hit shows how to invoke the
command.
&lt;/p&gt;
&lt;p&gt;
&lt;a title="http://msdn.microsoft.com/en-us/library/dd179457.aspx" href="http://msdn.microsoft.com/en-us/library/dd179457.aspx"&gt;http://msdn.microsoft.com/en-us/library/dd179457.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
To configure development storage against the default SQL Server instance:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;DSInit /sqlInstance:.&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;p&gt;
The program DSInit.exe is found in the Azure SDK: 
&lt;/p&gt;
&lt;p&gt;
C:\Program Files\Windows Azure SDK\v1.0\bin\devstore\
&lt;/p&gt;
&lt;p&gt;
Once I ran that program to initialize the local development app storage, I was off
and running with my cloud development tasks.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=fdd5d3da-f623-4962-b321-fa18c0ab3871" /&gt;</description>
      <comments>http://www.a7drew.com/blog/CommentView,guid,fdd5d3da-f623-4962-b321-fa18c0ab3871.aspx</comments>
      <category>Azure</category>
      <category>SQL Server</category>
      <category>Visual Studio</category>
    </item>
    <item>
      <trackback:ping>http://www.a7drew.com/blog/Trackback.aspx?guid=0da8418b-fa06-4646-95e4-539ca754d570</trackback:ping>
      <pingback:server>http://www.a7drew.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.a7drew.com/blog/PermaLink,guid,0da8418b-fa06-4646-95e4-539ca754d570.aspx</pingback:target>
      <dc:creator>Andrew Hay</dc:creator>
      <wfw:comment>http://www.a7drew.com/blog/CommentView,guid,0da8418b-fa06-4646-95e4-539ca754d570.aspx</wfw:comment>
      <wfw:commentRss>http://www.a7drew.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=0da8418b-fa06-4646-95e4-539ca754d570</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Behind the scenes, I see a lot of companies run on Excel and duct tape. So it's common
for clients to hand me a slab of data organized into neat little rows and columns.
Excel is just a handy way to throw some data over the wall and get things done. This
might be a list of dealers, a collection of user profiles, product information, or
anything that just needs to get somewhere else.
</p>
        <p>
Depending on the scenario, I'll might massage this data and slide it into a SQL Server
database or an XML file. I'm a web developer so I use the ADO.Net stack on a regular
basis. If I were a Windows client developer, I might prefer to solve this problem
with the Excel object model, but that really looks like more code to me, so here's
how I like to roll:
</p>
        <p>
The following code block accepts an Excel file path and returns an ordinary DataTable
object, which can be manipulated easily by the code that calls this method. The first
row of the Excel document becomes the columns in the DataTable object and each row
thereafter are DataRow objects.
</p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/ImportingDataFromExcelwithC_7CBC/ImportDataFromExcel_2.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="148" alt="ImportDataFromExcel" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/ImportingDataFromExcelwithC_7CBC/ImportDataFromExcel_thumb.png" width="244" border="0" />
          </a>
          <br />
(Click to enlarge)
</p>
        <p>
If you like, <a href="http://support.microsoft.com/kb/306023">take a look at both
techniques for working with Excel data</a> and see what one speaks to you. With this
block of code, I can happily accept large chunks of data from a client without spending
precious time fiddling with administrivia.
</p>
        <img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=0da8418b-fa06-4646-95e4-539ca754d570" />
      </body>
      <title>Importing Data From Excel In C#</title>
      <guid isPermaLink="false">http://www.a7drew.com/blog/PermaLink,guid,0da8418b-fa06-4646-95e4-539ca754d570.aspx</guid>
      <link>http://www.a7drew.com/blog/2008/07/31/ImportingDataFromExcelInC.aspx</link>
      <pubDate>Thu, 31 Jul 2008 16:00:16 GMT</pubDate>
      <description>&lt;p&gt;
Behind the scenes, I see a lot of companies run on Excel and duct tape. So it's common
for clients to hand me a slab of data organized into neat little rows and columns.
Excel is just a handy way to throw some data over the wall and get things done. This
might be a list of dealers, a collection of user profiles, product information, or
anything that just needs to get somewhere else.
&lt;/p&gt;
&lt;p&gt;
Depending on the scenario, I'll might massage this data and slide it into a SQL Server
database or an XML file. I'm a web developer so I use the ADO.Net stack on a regular
basis. If I were a Windows client developer, I might prefer to solve this problem
with the Excel object model, but that really looks like more code to me, so here's
how I like to roll:
&lt;/p&gt;
&lt;p&gt;
The following code block accepts an Excel file path and returns an ordinary DataTable
object, which can be manipulated easily by the code that calls this method. The first
row of the Excel document becomes the columns in the DataTable object and each row
thereafter are DataRow objects.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/ImportingDataFromExcelwithC_7CBC/ImportDataFromExcel_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="148" alt="ImportDataFromExcel" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/ImportingDataFromExcelwithC_7CBC/ImportDataFromExcel_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;br /&gt;
(Click to enlarge)
&lt;/p&gt;
&lt;p&gt;
If you like, &lt;a href="http://support.microsoft.com/kb/306023"&gt;take a look at both
techniques for working with Excel data&lt;/a&gt; and see what one speaks to you. With this
block of code, I can happily accept large chunks of data from a client without spending
precious time fiddling with administrivia.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=0da8418b-fa06-4646-95e4-539ca754d570" /&gt;</description>
      <comments>http://www.a7drew.com/blog/CommentView,guid,0da8418b-fa06-4646-95e4-539ca754d570.aspx</comments>
      <category>asp.net</category>
      <category>software</category>
      <category>SQL Server</category>
      <category>Visual Studio</category>
    </item>
    <item>
      <trackback:ping>http://www.a7drew.com/blog/Trackback.aspx?guid=6fa66dea-4cad-4891-9edf-0ddd02bdc551</trackback:ping>
      <pingback:server>http://www.a7drew.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.a7drew.com/blog/PermaLink,guid,6fa66dea-4cad-4891-9edf-0ddd02bdc551.aspx</pingback:target>
      <dc:creator>Andrew Hay</dc:creator>
      <wfw:comment>http://www.a7drew.com/blog/CommentView,guid,6fa66dea-4cad-4891-9edf-0ddd02bdc551.aspx</wfw:comment>
      <wfw:commentRss>http://www.a7drew.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=6fa66dea-4cad-4891-9edf-0ddd02bdc551</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I spent a little too long on this rainy Sunday afternoon tripping over myself. I'm
using the SqlDataSource and the FormView controls in a quick prototype. I clicked
the "Use optimistic concurrency" field in the SqlDataSource configuration page so
it would compare the original values before updating the database. 
</p>
        <p>
I was going along fine when I realized the updates didn't work, but the insert was
OK. After several repeated attempts with zero rows updates, I fired up the SQL Profiler
and saw my update query right there, plain as day. Why wasn't the update working?
</p>
        <p>
          <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="383" alt="sql-profiler" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/MillisecondUpdateProblemwithSqlDataSourc_10455/sql-profiler_3.png" width="590" border="0" />
        </p>
        <p>
I even grabbed the SQL out of SQL Profiler and ran it through Query Analyzer - and
then bam! I could see it on my screen. The milliseconds for the date time fields were
all set to zero, and the actual values in the database were non-zero values. Who was
loosing the milliseconds?
</p>
        <p>
Finally, it hit me. I was losing the data.
</p>
        <p>
I was trying to be real smart about setting the hidden "Created" and "Modified" DateTime
fields during the updating event of the SqlDataSource control. I would do a similar
assignment during the update event for just the "Modified" DateTime field.
</p>
        <pre class="brush: js">
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
   DateTime now = DateTime.Now;

   e.Command.Parameters["@Created"].Value = now;
   e.Command.Parameters["@Modified"].Value = now;
}
</pre>
        <p>
As I was peering at this method, it struck me that I was inadvertently setting the
fields to a value that was too granular for my SqlDataSource object. It wasn't passing
back the millisecond value, so I compensated by making sure the value is always zero
milliseconds, like so:
</p>
        <pre style="border-right: #999999 1px dashed; padding-right: 5px; border-top: #999999 1px dashed; padding-left: 5px; font-size: 12px; padding-bottom: 5px; border-left: #999999 1px dashed; width: 100%; color: #000000; line-height: 14px; padding-top: 5px; border-bottom: #999999 1px dashed; font-family: andale mono, lucida console, monaco, fixed, monospace; background-color: #eee">
          <code>protected
void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e) { DateTime
now = DateTime.Now; now = now.AddMilliseconds(1000 - now.Millisecond); e.Command.Parameters["@Created"].Value
= now; e.Command.Parameters["@Modified"].Value = now; } </code>
        </pre>
        <p>
With this adjustment, my inserts and updates are playing nicely. Yay! 
</p>
        <p>
So, now that i works, I'm still not real happy with it. I'd much rather have the code
check a single timestamp column named "Version" than see all that bloat in there.
</p>
        <img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=6fa66dea-4cad-4891-9edf-0ddd02bdc551" />
      </body>
      <title>Millisecond Update Problem with SqlDataSource</title>
      <guid isPermaLink="false">http://www.a7drew.com/blog/PermaLink,guid,6fa66dea-4cad-4891-9edf-0ddd02bdc551.aspx</guid>
      <link>http://www.a7drew.com/blog/2008/03/24/MillisecondUpdateProblemWithSqlDataSource.aspx</link>
      <pubDate>Mon, 24 Mar 2008 01:30:47 GMT</pubDate>
      <description>&lt;p&gt;
I spent a little too long on this rainy Sunday afternoon tripping over myself. I'm
using the SqlDataSource and the FormView controls in a quick prototype. I clicked
the "Use optimistic concurrency" field in the SqlDataSource configuration page so
it would compare the original values before updating the database. 
&lt;/p&gt;
&lt;p&gt;
I was going along fine when I realized the updates didn't work, but the insert was
OK. After several repeated attempts with zero rows updates, I fired up the SQL Profiler
and saw my update query right there, plain as day. Why wasn't the update working?
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="383" alt="sql-profiler" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/MillisecondUpdateProblemwithSqlDataSourc_10455/sql-profiler_3.png" width="590" border="0" /&gt;
&lt;/p&gt;
&lt;p&gt;
I even grabbed the SQL out of SQL Profiler and ran it through Query Analyzer - and
then bam! I could see it on my screen. The milliseconds for the date time fields were
all set to zero, and the actual values in the database were non-zero values. Who was
loosing the milliseconds?
&lt;/p&gt;
&lt;p&gt;
Finally, it hit me. I was losing the data.
&lt;/p&gt;
&lt;p&gt;
I was trying to be real smart about setting the hidden "Created" and "Modified" DateTime
fields during the updating event of the SqlDataSource control. I would do a similar
assignment during the update event for just the "Modified" DateTime field.
&lt;/p&gt;
&lt;pre class="brush: js"&gt;
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
   DateTime now = DateTime.Now;

   e.Command.Parameters["@Created"].Value = now;
   e.Command.Parameters["@Modified"].Value = now;
}
&lt;/pre&gt;
&lt;p&gt;
As I was peering at this method, it struck me that I was inadvertently setting the
fields to a value that was too granular for my SqlDataSource object. It wasn't passing
back the millisecond value, so I compensated by making sure the value is always zero
milliseconds, like so:
&lt;/p&gt;
&lt;pre style="border-right: #999999 1px dashed; padding-right: 5px; border-top: #999999 1px dashed; padding-left: 5px; font-size: 12px; padding-bottom: 5px; border-left: #999999 1px dashed; width: 100%; color: #000000; line-height: 14px; padding-top: 5px; border-bottom: #999999 1px dashed; font-family: andale mono, lucida console, monaco, fixed, monospace; background-color: #eee"&gt;&lt;code&gt;protected
void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e) { DateTime
now = DateTime.Now; now = now.AddMilliseconds(1000 - now.Millisecond); e.Command.Parameters["@Created"].Value
= now; e.Command.Parameters["@Modified"].Value = now; } &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
With this adjustment, my inserts and updates are playing nicely. Yay! 
&lt;/p&gt;
&lt;p&gt;
So, now that i works, I'm still not real happy with it. I'd much rather have the code
check a single timestamp column named "Version" than see all that bloat in there.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=6fa66dea-4cad-4891-9edf-0ddd02bdc551" /&gt;</description>
      <comments>http://www.a7drew.com/blog/CommentView,guid,6fa66dea-4cad-4891-9edf-0ddd02bdc551.aspx</comments>
      <category>asp.net</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.a7drew.com/blog/Trackback.aspx?guid=e56335bf-89d5-4cac-900c-73583fb5dba7</trackback:ping>
      <pingback:server>http://www.a7drew.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.a7drew.com/blog/PermaLink,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</pingback:target>
      <dc:creator>Andrew Hay</dc:creator>
      <wfw:comment>http://www.a7drew.com/blog/CommentView,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</wfw:comment>
      <wfw:commentRss>http://www.a7drew.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=e56335bf-89d5-4cac-900c-73583fb5dba7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
LINQ is an acronym for Language-Integrated Query and a new feature in v3.5 of the
.Net Framework from Microsoft. This new version of Microsoft .Net reached RTM status
a couple of weeks ago — this framework is chock full of brilliant things I can
use to improve my efficiency and effectiveness on daily tasks here at Pop Art.
</p>
        <p>
As one of my favorite features, LINQ helps me write data-driven application even faster
than what .Net 2.0 brought to the table. A common example starts with launching Visual
Studio 2008. After dragging a collection of tables from a SQL Server database onto
a surface in my solution, I can see a visualization of the columns in the tables as
well as the relationships between them. Click the following thumbnail to see a larger
image.
</p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="196" alt="database schema" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
Next, I start writing data access code directly in my C# program as opposed to switching
languages and writing in the T-SQL language. Visual Studio gives me Intellisense here
too; as I type the name of a table and click the period key, all of the columns in
the table appear where the cursor is located. Big time saver. Huge! I'm certain that
I didn't misspell a column name and that my code will compile.
</p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="192" alt="intellisense is beautiful" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
The syntax for LINQ in a C# program is very similar to the T-SQL language, which is
a "set based" language. LINQ statements are compiled, just like the rest
of my C# code. The first thing someone well versed in T-SQL will notice is that the
columns normally specified in a SELECT query are at the end of the LINQ statement
instead of the start as in T-SQL. The idea is that you're articulating your constraints
at the start of the LINQ statement and then pulling out the fields you need at the
very end.
</p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="169" alt="a LINQ sample" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
Behind the scenes, LINQ is using the relationships expressed in the database to generate
T-SQL scripts on the fly. This is a clear line of demarcation for LINQ. If you're
using stored procedures exclusively for database access, then LINQ isn't going to
buy you much. You'll still get Intellisense inside Visual Studio and you can specify
an existing stored procedure instead of using the auto-generated SQL, but you're giving
up a lot of acceleration tools. Perhaps more than you're getting in return.
</p>
        <p>
LINQ really shines in multiple table joins and aggregation. The following two blocks
of code show a query is performed in LINQ and its equivalent T-SQL script. The query
retrieves product information from three tables where the list price is below a given
amount and a sub-category name exists. If you go the T-SQL route, you will still need
to write some C# code to call your database query.
</p>
        <p>
Which of the following versions would you rather author and support?
</p>
        <p>
          <strong>LINQ Version</strong>
        </p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="201" alt="a more complex LINQ statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
          <strong>T-SQL Version</strong>
        </p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_4.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="154" alt="an equivalent T-SQL statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_thumb_1.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
MSDN is a great online resource for developers and they really hit a home-run here.
They have a page with <a href="http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx" target="_blank">101
LINQ samples</a>. This is my preferred way to learn when I already know the surrounding
technologies and I want to fill in a specific gap. The page categorizes several ways
of retrieving and iterating over information. 
</p>
        <img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=e56335bf-89d5-4cac-900c-73583fb5dba7" />
      </body>
      <title>Understanding LINQ</title>
      <guid isPermaLink="false">http://www.a7drew.com/blog/PermaLink,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</guid>
      <link>http://www.a7drew.com/blog/2007/12/19/UnderstandingLINQ.aspx</link>
      <pubDate>Wed, 19 Dec 2007 01:36:23 GMT</pubDate>
      <description>&lt;p&gt;
LINQ is an acronym for Language-Integrated Query and a new feature in v3.5 of the
.Net Framework from Microsoft. This new version of Microsoft .Net reached RTM status
a couple of weeks ago &amp;#8212; this framework is chock full of brilliant things I can
use to improve my efficiency and effectiveness on daily tasks here at Pop Art.
&lt;/p&gt;
&lt;p&gt;
As one of my favorite features, LINQ helps me write data-driven application even faster
than what .Net 2.0 brought to the table. A common example starts with launching Visual
Studio 2008. After dragging a collection of tables from a SQL Server database onto
a surface in my solution, I can see a visualization of the columns in the tables as
well as the relationships between them. Click the following thumbnail to see a larger
image.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="196" alt="database schema" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Next, I start writing data access code directly in my C# program as opposed to switching
languages and writing in the T-SQL language. Visual Studio gives me Intellisense here
too; as I type the name of a table and click the period key, all of the columns in
the table appear where the cursor is located. Big time saver. Huge! I'm certain that
I didn't misspell a column name and that my code will compile.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="192" alt="intellisense is beautiful" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The syntax for LINQ in a C# program is very similar to the T-SQL language, which is
a &amp;quot;set based&amp;quot; language. LINQ statements are compiled, just like the rest
of my C# code. The first thing someone well versed in T-SQL will notice is that the
columns normally specified in a SELECT query are at the end of the LINQ statement
instead of the start as in T-SQL. The idea is that you're articulating your constraints
at the start of the LINQ statement and then pulling out the fields you need at the
very end.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="169" alt="a LINQ sample" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Behind the scenes, LINQ is using the relationships expressed in the database to generate
T-SQL scripts on the fly. This is a clear line of demarcation for LINQ. If you're
using stored procedures exclusively for database access, then LINQ isn't going to
buy you much. You'll still get Intellisense inside Visual Studio and you can specify
an existing stored procedure instead of using the auto-generated SQL, but you're giving
up a lot of acceleration tools. Perhaps more than you're getting in return.
&lt;/p&gt;
&lt;p&gt;
LINQ really shines in multiple table joins and aggregation. The following two blocks
of code show a query is performed in LINQ and its equivalent T-SQL script. The query
retrieves product information from three tables where the list price is below a given
amount and a sub-category name exists. If you go the T-SQL route, you will still need
to write some C# code to call your database query.
&lt;/p&gt;
&lt;p&gt;
Which of the following versions would you rather author and support?
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;LINQ Version&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="201" alt="a more complex LINQ statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;T-SQL Version&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_4.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="154" alt="an equivalent T-SQL statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_thumb_1.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
MSDN is a great online resource for developers and they really hit a home-run here.
They have a page with &lt;a href="http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx" target="_blank"&gt;101
LINQ samples&lt;/a&gt;. This is my preferred way to learn when I already know the surrounding
technologies and I want to fill in a specific gap. The page categorizes several ways
of retrieving and iterating over information. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=e56335bf-89d5-4cac-900c-73583fb5dba7" /&gt;</description>
      <comments>http://www.a7drew.com/blog/CommentView,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</comments>
      <category>LINQ</category>
      <category>SQL Server</category>
      <category>Visual Studio</category>
    </item>
  </channel>
</rss>