<?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>The Wit and Ramblings of David Giard - Back To Basics</title>
    <link>http://www.davidgiard.com/</link>
    <description>Demanding rigidly defined areas of doubt and uncertainty</description>
    <language>en-us</language>
    <copyright>David Giard</copyright>
    <lastBuildDate>Wed, 30 May 2012 16:21:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>davidgiard@davidgiard.com</managingEditor>
    <webMaster>davidgiard@davidgiard.com</webMaster>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=600474fd-28d9-4dcd-91ec-57d1aca5a8a1</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,600474fd-28d9-4dcd-91ec-57d1aca5a8a1.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,600474fd-28d9-4dcd-91ec-57d1aca5a8a1.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=600474fd-28d9-4dcd-91ec-57d1aca5a8a1</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
 
</p>
        <p>
In .NET applications, Connection Strings are easily stored in and retrieved from a <em>web.config </em>file
or an <em>app.config </em>file. Add a <font face="Consolas">&lt;connectionStrings&gt; </font>section
to the <em>config </em>file, directly within the <font face="Consolas">&lt;configuration&gt;</font> section.
Within the <font face="Consolas">&lt;connectionStrings&gt; </font>section, place an <font face="Consolas">&lt;add&gt;</font> tag
for each connection string you wish to store and retrieve. This <font face="Consolas">&lt;add&gt;</font> tag
contains two important attributes: <em>name</em> and <em>connectionString</em>. The <em>name </em>attribute
will be used to look up an entry, so each <font face="Consolas">&lt;add&gt;</font> tag
should have a unique name. The <em>connectionString </em>attribute contains the connection
string you will use to connect to a database. If your application needs to connect
to a number of different data types, it is worth setting the Provider property as
well. This is the name of a database provider installed on the current machine, that
will allow you to connect to a database. 
<br />
An example is shown below:
</p>
        <pre class="csharpcode">&lt;configuration&gt;
  &lt;connectionStrings&gt;
    &lt;add name=<span class="str">"MyApp_Dev"</span><br />
        connectionString=<span class="str">"Data
Source=Server01;Initial Catalog=AwesomeDB_Dev;Integrated Security=True"</span>/&gt;
&lt;add name=<span class="str">"MyApp_QA"</span><br />
        connectionString=<span class="str">"Data
Source=Server01;Initial Catalog=AwesomeDB_QA;Integrated Security=True"</span>/&gt;
&lt;add name=<span class="str">"MyApp_Prod"</span><br />
        connectionString=<span class="str">"Data
Source=Server01;Initial Catalog=AwesomeDB;Integrated Security=True"</span>/&gt;
&lt;/connectionStrings&gt; &lt;/configuration&gt;</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>
To retrieve a connection string by its associated <em>name </em>attribute, use the
utilities found in the <em>System.Configuration </em>namespace of the System.Configuration
assembly. First, set a reference to <em>System.Assembly </em>by selecting the menu
option Project | Add Reference, selecting the System.Assembly in the Component Name
column of the .NET tab of the Add Reference dialog; and, clicking the OK button. Then,
add the following using statement to the top of your class file.
</p>
        <p>
This namespace contains the static <em>ConfigurationManager</em> class, which exposes
a number of methods for retrieving configuration information. <font face="Courier New">ConfigurationManager.ConnectionStrings </font>returns
a collection containing all connection strings settings in the <em>config </em>file
of the current assembly. Once we have this collection, we can retrieve a single item
by specifying its name. Doing so returns a <em>ConnectionStringSettings</em> object
that contains properties retrieved from a single &lt;add&gt; element of the <em>config </em>file.
The important property is <em>ConnectionString</em>, which can be used with ADO.NET
to connect to a database. Knowing the Provider property may also be useful if your
application connects to different types of data. 
<br />
Sample code is below. 
<br /></p>
        <pre class="csharpcode">ConnectionStringSettingsCollection cssCollection =
    ConfigurationManager.ConnectionStrings;
ConnectionStringSettings devCsSettings = cssCollection[<span class="str">"MyApp_Dev"</span>]; <span class="kwrd">string</span> devCs
= devCsSettings.ConnectionString;</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>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=600474fd-28d9-4dcd-91ec-57d1aca5a8a1" />
      </body>
      <title>Reading a Connection String from a CONFIG file</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,600474fd-28d9-4dcd-91ec-57d1aca5a8a1.aspx</guid>
      <link>http://www.davidgiard.com/2012/05/30/ReadingAConnectionStringFromACONFIGFile.aspx</link>
      <pubDate>Wed, 30 May 2012 16:21:00 GMT</pubDate>
      <description>&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
In .NET applications, Connection Strings are easily stored in and retrieved from a &lt;em&gt;web.config &lt;/em&gt;file
or an &lt;em&gt;app.config &lt;/em&gt;file. Add a &lt;font face="Consolas"&gt;&amp;lt;connectionStrings&amp;gt; &lt;/font&gt;section
to the &lt;em&gt;config &lt;/em&gt;file, directly within the &lt;font face="Consolas"&gt;&amp;lt;configuration&amp;gt;&lt;/font&gt; section.
Within the &lt;font face="Consolas"&gt;&amp;lt;connectionStrings&amp;gt; &lt;/font&gt;section, place an &lt;font face="Consolas"&gt;&amp;lt;add&amp;gt;&lt;/font&gt; tag
for each connection string you wish to store and retrieve. This &lt;font face="Consolas"&gt;&amp;lt;add&amp;gt;&lt;/font&gt; tag
contains two important attributes: &lt;em&gt;name&lt;/em&gt; and &lt;em&gt;connectionString&lt;/em&gt;. The &lt;em&gt;name &lt;/em&gt;attribute
will be used to look up an entry, so each &lt;font face="Consolas"&gt;&amp;lt;add&amp;gt;&lt;/font&gt; tag
should have a unique name. The &lt;em&gt;connectionString &lt;/em&gt;attribute contains the connection
string you will use to connect to a database. If your application needs to connect
to a number of different data types, it is worth setting the Provider property as
well. This is the name of a database provider installed on the current machine, that
will allow you to connect to a database. 
&lt;br /&gt;
An example is shown below:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&amp;lt;configuration&amp;gt;
  &amp;lt;connectionStrings&amp;gt;
    &amp;lt;add name=&lt;span class="str"&gt;&amp;quot;MyApp_Dev&amp;quot;&lt;/span&gt; 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connectionString=&lt;span class="str"&gt;&amp;quot;Data
Source=Server01;Initial Catalog=AwesomeDB_Dev;Integrated Security=True&amp;quot;&lt;/span&gt;/&amp;gt;
&amp;lt;add name=&lt;span class="str"&gt;&amp;quot;MyApp_QA&amp;quot;&lt;/span&gt; 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connectionString=&lt;span class="str"&gt;&amp;quot;Data
Source=Server01;Initial Catalog=AwesomeDB_QA;Integrated Security=True&amp;quot;&lt;/span&gt;/&amp;gt;
&amp;lt;add name=&lt;span class="str"&gt;&amp;quot;MyApp_Prod&amp;quot;&lt;/span&gt; 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connectionString=&lt;span class="str"&gt;&amp;quot;Data
Source=Server01;Initial Catalog=AwesomeDB;Integrated Security=True&amp;quot;&lt;/span&gt;/&amp;gt;
&amp;lt;/connectionStrings&amp;gt; &amp;lt;/configuration&amp;gt;&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;
To retrieve a connection string by its associated &lt;em&gt;name &lt;/em&gt;attribute, use the
utilities found in the &lt;em&gt;System.Configuration &lt;/em&gt;namespace of the System.Configuration
assembly. First, set a reference to &lt;em&gt;System.Assembly &lt;/em&gt;by selecting the menu
option Project | Add Reference, selecting the System.Assembly in the Component Name
column of the .NET tab of the Add Reference dialog; and, clicking the OK button. Then,
add the following using statement to the top of your class file.
&lt;/p&gt;
&lt;p&gt;
This namespace contains the static &lt;em&gt;ConfigurationManager&lt;/em&gt; class, which exposes
a number of methods for retrieving configuration information. &lt;font face="Courier New"&gt;ConfigurationManager.ConnectionStrings &lt;/font&gt;returns
a collection containing all connection strings settings in the &lt;em&gt;config &lt;/em&gt;file
of the current assembly. Once we have this collection, we can retrieve a single item
by specifying its name. Doing so returns a &lt;em&gt;ConnectionStringSettings&lt;/em&gt; object
that contains properties retrieved from a single &amp;lt;add&amp;gt; element of the &lt;em&gt;config &lt;/em&gt;file.
The important property is &lt;em&gt;ConnectionString&lt;/em&gt;, which can be used with ADO.NET
to connect to a database. Knowing the Provider property may also be useful if your
application connects to different types of data. 
&lt;br /&gt;
Sample code is below. 
&lt;br /&gt;
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;ConnectionStringSettingsCollection cssCollection =
    ConfigurationManager.ConnectionStrings;
ConnectionStringSettings devCsSettings = cssCollection[&lt;span class="str"&gt;&amp;quot;MyApp_Dev&amp;quot;&lt;/span&gt;]; &lt;span class="kwrd"&gt;string&lt;/span&gt; devCs
= devCsSettings.ConnectionString;&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;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=600474fd-28d9-4dcd-91ec-57d1aca5a8a1" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,600474fd-28d9-4dcd-91ec-57d1aca5a8a1.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=bfbb8a12-54aa-412f-b0bc-45d46285b722</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,bfbb8a12-54aa-412f-b0bc-45d46285b722.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,bfbb8a12-54aa-412f-b0bc-45d46285b722.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=bfbb8a12-54aa-412f-b0bc-45d46285b722</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
I remember how excited I was in the early days of .Net when I discovered how easy
it was to write a Windows Service. I probably wrote a half dozen services that first
year. But I hadn't written one in years and (oddly) hadn't even heard anyone talking
about writing a Windows service in as long.
</p>
        <p>
Perhaps the reason one hears so little about Windows Services is because the way we
write them has changed so little since .Net 1.0.
</p>
        <p>
A Windows Service is not dependent on a particular user being logged in. In fact,
a Windows Service can run if no one is currently logged onto the machine on which
it is running. This makes a Windows Service inherently more secure than a Windows
Forms application or a Console application because you can run it on a server, set
credentials and log out of the console.
</p>
        <p>
To create a Windows Service, open Visual Studio and select <font face="Courier New">File
| New Project</font>. In the <strong>Project Types </strong>tree of the <strong>New
Project </strong>dialog, expand either <strong>Visual C#</strong> or <strong>Visual
Basic </strong>node and select the <strong>Windows </strong>node. In the <strong>Templates </strong>area
of the dialog, select <strong>Windows Service</strong>. Set the Name, Location and
Solution as you would any other project and click <strong>OK</strong> to create the
project.
</p>
        <p>
By default, a Windows service contains one class named "Service1". The name of this
class isn't really important because it isn't called externally, so I always leave
this default name. If you double-click this class in the Solution Explorer, it opens
in a Design view. Select <font face="Courier New">View | Code </font>to switch to
a code view of the class. Notice that the class inherits from <em>ServiceBase </em>and
that it contains overrides of two methods: <em>OnStart </em>and <em>OnStop</em>.
</p>
        <p>
As you might guess, <em>OnStart </em>contains code that runs when a service starts
and <em>OnStop </em>contains code that runs when a service stops. I put setup code
into <em>OnStart</em>, such as initializing variables that my service will need. I
generally put very little code in the <em>OnStop </em>method, but this is where cleanup
code goes.
</p>
        <p>
Services are designed for long-running processes and are meant to stay in memory for
a long time -sometimes months or years. Most of the services I've written use a timer
object to periodically wake up and perform some check and respond if that check finds
something. For example, you might check the contents of a folder every 10 minutes
and, if an XML file is found in that folder, move it to a new location and parse it
appropriately. 
</p>
        <p>
For example I  recently wanted a program that would check an error log every
few minutes and automatically attempt to correct any errors found there. I had already
written a class to read the error log, retry each error found and remove from the
log any errors that were retried successfully. So my service only needed to call this
class every 5 minutes. I used a timer class to do this. A partial code listing is
shown below.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">protected</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">override</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> OnStart(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span>[]
args) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
Every 30 seconds, a timer will do some work</span> timer1.Elapsed += <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> ElapsedEventHandler(timer1_Elapsed);
timer1.Interval <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 30000;
timer1.Enabled <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">true</span>;
timer1.Start(); } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">protected</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">override</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> OnStop()
{ timer1.Enabled <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">false</span>;
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> timer1_Elapsed(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">object</span> sender,
EventArgs e) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
Wake up and perform some action.</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
[Cpde omitted]</span> } </span>
        </pre>
        <p>
I prefer to keep the code in the service to a minimum and abstract complex logic to
a separate assembly. This makes testing and debugging easier. So the omitted code
in the above example would call out to another assembly to do the hard work.
</p>
        <p>
          <strong>Installing a Service<br /></strong>In order to install a service, you will need to add an <em>Installer </em>class.
Select <font face="Courier New">Project | Add New Item</font>; then select the <strong>Installer
Class </strong>template. This class also opens in the designer by default. Select <font face="Courier New">View
| Code </font>to see the code. The <em>Installer </em>class inherits from Installer,
but you don't need to override any methods. 
</p>
        <p>
You can set some attributes of the service to make it easier to find. The <em>Installer
c</em>lass's constructor is the place to do this. Instantiate a new <em>ServiceInstaller </em>and <em>ServiceProcessInstaller</em>,
set properties of these objects, and add these objects to the <em>Installer </em>Class
to affect the Windows Service when it is installed. Common properties that I like
to set are
</p>
        <table>
          <thead>
            <tr>
              <td>
                <strong>Class</strong>
              </td>
              <td>
                <strong>Property</strong>
              </td>
              <td>
                <strong>Description of property</strong>
              </td>
            </tr>
            <tr>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
ServiceInstaller 
</td>
              <td>
ServiceInstallerServiceName 
</td>
              <td>
The name of the service. This must match the ServiceName specified in WindowsService1. 
</td>
            </tr>
            <tr>
              <td>
ServiceInstaller 
</td>
              <td>
DisplayName 
</td>
              <td>
A name displayed in the Services Manager applet. 
</td>
            </tr>
            <tr>
              <td>
ServiceInstaller 
</td>
              <td>
Description 
</td>
              <td>
A description displayed in the Services Manager applet 
</td>
            </tr>
            <tr>
              <td>
ServiceProcessInstaller 
</td>
              <td>
Account 
</td>
              <td>
A built-in account under which the service will run. 
</td>
            </tr>
            <tr>
              <td>
ServiceProcessInstaller 
</td>
              <td>
Username 
</td>
              <td>
The name of the user account under which the service will run. 
</td>
            </tr>
            <tr>
              <td>
ServiceProcessInstaller 
</td>
              <td>
Password 
</td>
              <td>
The password of the user account under which the service will run. 
</td>
            </tr>
          </tbody>
        </table>
        <p>
For the <em>ServiceProcessInstaller</em>, you will set either the <em>Account </em>property
or the <em>UserName </em>and <em>Password </em>properties. Typically, I set the <em>Account </em>property
to <em>System.ServiceProcess.ServiceAccount.LocalSystem</em>, so that it can be installed.
This account probably won't have sufficient privileges to accomplish what my code
is trying to do, so someone will need to open the Services Manager and change this
to a valid account. I could hard-code the <em>Name </em>and <em>Password </em>of an
account that I know has sufficient privileges, but this ties my application too tightly
to a single domain or server or organization. I would rather keep it flexible enough
that it can run anywhere. And besides, the account under which a service runs is really
a deployment issue, so others should be making these decisions and they should be
forced to think about this at deployment time.
</p>
        <p>
Below is sample code for the installer class
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">[RunInstaller(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">true</span>)] <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> partial <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Installer1
: Installer { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> Installer1()
{ InitializeComponent(); ServiceInstaller si <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> ServiceInstaller();
ServiceProcessInstaller spi <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> ServiceProcessInstaller();
si.ServiceName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"DGWinSvc"</span>; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
this must match the ServiceName specified in Service1.</span> si.DisplayName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"DGWinSvc"</span>; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
this will be displayed in the Services Manager.</span> si.Description <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"A
test service that takes some action every 30 seconds"</span>; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">this</span>.Installers.Add(si);
spi.Account <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> System.ServiceProcess.ServiceAccount.LocalSystem; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
run under the system account.</span> spi.Password <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">null</span>;
spi.Username <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">null</span>; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">this</span>.Installers.Add(spi);
} } </span>
        </pre>
        <p>
After your code is tested and compiled, you can deploy it to a server. Copy to a location
on the server the service EXE and any associated DLLs, Config files or other objects
required for it to run. The server must have the .Net framework installed. If it has
the framework, it should have a program called InstallUtil.exe. You can find this
program in the Windows folder under <em>Microsoft.NET\Framework </em>in the subfolder
corresponding to the .Net CLR version under which you compiled the service. On my
server, I found InstallUtil.exe in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727.
Open a command prompt, change to the location of InstallUtil.exe and run the following
command<br />
INSTALLUTIL &lt;full path of Windows Service Executable&gt;
</p>
        <p>
You can later uninstall the service with the following command<br /></p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">INSTALLUTIL
/u &lt;full path of Windows Service Executable&gt;</span>
        </pre>
        <p>
Now open the <em>Services Manager </em>applet (under Windows <em>Administrative Tools</em>)
and refresh the list of services. You should see your service listed with the <em>DisplayName </em>you
assigned in the <em>Installer </em>class. Right-click this service and select <strong>Properties </strong>to
display the <strong>Service Properties </strong>dialog. Click the "<strong>Log On</strong>"
tab, select "A particular user" and enter the name and password of the user under
which this service should run. You may need to create this user. It should have permission
to access all resources that the service needs to access. Click the <strong>OK </strong>button
to save these changes and close the dialog.
</p>
        <p>
Of course, you can also write a setup project to your solution if you want to automate
the deployment process. This article does not cover that.
</p>
        <p>
To start the service, right-click the service in the <em>Services Manager </em>and
select <strong>Start</strong>.  You can use the same applet to stop the service.
Right-click its name and select <strong>Stop</strong>. Alternatively, you can stop
and start the service from the command line with the following commands
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">NET
START &lt;ServiceName&gt; NET STOP &lt;ServiceName&gt;</span>
        </pre>
        <p>
where &lt;ServiceName&gt; s the ServiceInstaller ServiceName property specified in
our installer class. For our example, we would type. This is a useful if you want
to script the starting and stopping of your service.
</p>
        <p>
Windows Services are something that .Net got right very early and hasn't needed to
change. Creating useful services is easy with the .Net framework.
</p>
        <p>
You can download a simple Windows service at <a href="http://www.davidgiard.com/content/binary/DGWinSvc.zip">DGWinSvc.zip
(28.05 KB)</a>.
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=bfbb8a12-54aa-412f-b0bc-45d46285b722" />
      </body>
      <title>Writing a Windows Service in .Net</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,bfbb8a12-54aa-412f-b0bc-45d46285b722.aspx</guid>
      <link>http://www.davidgiard.com/2009/11/01/WritingAWindowsServiceInNet.aspx</link>
      <pubDate>Sun, 01 Nov 2009 04:40:13 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt;
&lt;/p&gt;
&lt;p&gt;
I remember how excited I was in the early days of .Net when I discovered how easy
it was to write a Windows Service. I probably wrote a half dozen services that first
year. But I hadn't written one in years and (oddly) hadn't even heard anyone talking
about writing a Windows service in as long.
&lt;/p&gt;
&lt;p&gt;
Perhaps the reason one hears so little about Windows Services is because the way we
write them has changed so little since .Net 1.0.
&lt;/p&gt;
&lt;p&gt;
A Windows Service is not dependent on a particular user being logged in. In fact,
a Windows Service can run if no one is currently logged onto the machine on which
it is running. This makes a Windows Service inherently more secure than a Windows
Forms application or a Console application because you can run it on a server, set
credentials and log out of the console.
&lt;/p&gt;
&lt;p&gt;
To create a Windows Service, open Visual Studio and select &lt;font face="Courier New"&gt;File
| New Project&lt;/font&gt;. In the &lt;strong&gt;Project Types &lt;/strong&gt;tree of the &lt;strong&gt;New
Project &lt;/strong&gt;dialog, expand either &lt;strong&gt;Visual C#&lt;/strong&gt; or &lt;strong&gt;Visual
Basic &lt;/strong&gt;node and select the &lt;strong&gt;Windows &lt;/strong&gt;node. In the &lt;strong&gt;Templates &lt;/strong&gt;area
of the dialog, select &lt;strong&gt;Windows Service&lt;/strong&gt;. Set the Name, Location and
Solution as you would any other project and click &lt;strong&gt;OK&lt;/strong&gt; to create the
project.
&lt;/p&gt;
&lt;p&gt;
By default, a Windows service contains one class named "Service1". The name of this
class isn't really important because it isn't called externally, so I always leave
this default name. If you double-click this class in the Solution Explorer, it opens
in a Design view. Select &lt;font face="Courier New"&gt;View | Code &lt;/font&gt;to switch to
a code view of the class. Notice that the class inherits from &lt;em&gt;ServiceBase &lt;/em&gt;and
that it contains overrides of two methods: &lt;em&gt;OnStart &lt;/em&gt;and &lt;em&gt;OnStop&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
As you might guess, &lt;em&gt;OnStart &lt;/em&gt;contains code that runs when a service starts
and &lt;em&gt;OnStop &lt;/em&gt;contains code that runs when a service stops. I put setup code
into &lt;em&gt;OnStart&lt;/em&gt;, such as initializing variables that my service will need. I
generally put very little code in the &lt;em&gt;OnStop &lt;/em&gt;method, but this is where cleanup
code goes.
&lt;/p&gt;
&lt;p&gt;
Services are designed for long-running processes and are meant to stay in memory for
a long time -sometimes months or years. Most of the services I've written use a timer
object to periodically wake up and perform some check and respond if that check finds
something. For example, you might check the contents of a folder every 10 minutes
and, if an XML file is found in that folder, move it to a new location and parse it
appropriately. 
&lt;/p&gt;
&lt;p&gt;
For example I&amp;nbsp; recently wanted a program that would check an error log every
few minutes and automatically attempt to correct any errors found there. I had already
written a class to read the error log, retry each error found and remove from the
log any errors that were retried successfully. So my service only needed to call this
class every 5 minutes. I used a timer class to do this. A partial code listing is
shown below.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;protected&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;override&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; OnStart(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt;[]
args) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
Every 30 seconds, a timer will do some work&lt;/span&gt; timer1.Elapsed += &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; ElapsedEventHandler(timer1_Elapsed);
timer1.Interval &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 30000;
timer1.Enabled &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;true&lt;/span&gt;;
timer1.Start(); } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;protected&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;override&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; OnStop()
{ timer1.Enabled &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;false&lt;/span&gt;;
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; timer1_Elapsed(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;object&lt;/span&gt; sender,
EventArgs e) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
Wake up and perform some action.&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
[Cpde omitted]&lt;/span&gt; } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
I prefer to keep the code in the service to a minimum and abstract complex logic to
a separate assembly. This makes testing and debugging easier. So the omitted code
in the above example would call out to another assembly to do the hard work.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Installing a Service&lt;br&gt;
&lt;/strong&gt;In order to install a service, you will need to add an &lt;em&gt;Installer &lt;/em&gt;class.
Select &lt;font face="Courier New"&gt;Project | Add New Item&lt;/font&gt;; then select the &lt;strong&gt;Installer
Class &lt;/strong&gt;template. This class also opens in the designer by default. Select &lt;font face="Courier New"&gt;View
| Code &lt;/font&gt;to see the code. The &lt;em&gt;Installer &lt;/em&gt;class inherits from Installer,
but you don't need to override any methods. 
&lt;/p&gt;
&lt;p&gt;
You can set some attributes of the service to make it easier to find. The &lt;em&gt;Installer
c&lt;/em&gt;lass's constructor is the place to do this. Instantiate a new &lt;em&gt;ServiceInstaller &lt;/em&gt;and &lt;em&gt;ServiceProcessInstaller&lt;/em&gt;,
set properties of these objects, and add these objects to the &lt;em&gt;Installer &lt;/em&gt;Class
to affect the Windows Service when it is installed. Common properties that I like
to set are
&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Class&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Property&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Description of property&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
ServiceInstaller 
&lt;/td&gt;
&lt;td&gt;
ServiceInstallerServiceName 
&lt;/td&gt;
&lt;td&gt;
The name of the service. This must match the ServiceName specified in WindowsService1. 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ServiceInstaller 
&lt;/td&gt;
&lt;td&gt;
DisplayName 
&lt;/td&gt;
&lt;td&gt;
A name displayed in the Services Manager applet. 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ServiceInstaller 
&lt;/td&gt;
&lt;td&gt;
Description 
&lt;/td&gt;
&lt;td&gt;
A description displayed in the Services Manager applet 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ServiceProcessInstaller 
&lt;/td&gt;
&lt;td&gt;
Account 
&lt;/td&gt;
&lt;td&gt;
A built-in account under which the service will run. 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ServiceProcessInstaller 
&lt;/td&gt;
&lt;td&gt;
Username 
&lt;/td&gt;
&lt;td&gt;
The name of the user account under which the service will run. 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ServiceProcessInstaller 
&lt;/td&gt;
&lt;td&gt;
Password 
&lt;/td&gt;
&lt;td&gt;
The password of the user account under which the service will run. 
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
For the &lt;em&gt;ServiceProcessInstaller&lt;/em&gt;, you will set either the &lt;em&gt;Account &lt;/em&gt;property
or the &lt;em&gt;UserName &lt;/em&gt;and &lt;em&gt;Password &lt;/em&gt;properties. Typically, I set the &lt;em&gt;Account &lt;/em&gt;property
to &lt;em&gt;System.ServiceProcess.ServiceAccount.LocalSystem&lt;/em&gt;, so that it can be installed.
This account probably won't have sufficient privileges to accomplish what my code
is trying to do, so someone will need to open the Services Manager and change this
to a valid account. I could hard-code the &lt;em&gt;Name &lt;/em&gt;and &lt;em&gt;Password &lt;/em&gt;of an
account that I know has sufficient privileges, but this ties my application too tightly
to a single domain or server or organization. I would rather keep it flexible enough
that it can run anywhere. And besides, the account under which a service runs is really
a deployment issue, so others should be making these decisions and they should be
forced to think about this at deployment time.
&lt;/p&gt;
&lt;p&gt;
Below is sample code for the installer class
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;[RunInstaller(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;true&lt;/span&gt;)] &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; partial &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Installer1
: Installer { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; Installer1()
{ InitializeComponent(); ServiceInstaller si &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; ServiceInstaller();
ServiceProcessInstaller spi &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; ServiceProcessInstaller();
si.ServiceName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"DGWinSvc"&lt;/span&gt;; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
this must match the ServiceName specified in Service1.&lt;/span&gt; si.DisplayName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"DGWinSvc"&lt;/span&gt;; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
this will be displayed in the Services Manager.&lt;/span&gt; si.Description &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"A
test service that takes some action every 30 seconds"&lt;/span&gt;; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;this&lt;/span&gt;.Installers.Add(si);
spi.Account &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; System.ServiceProcess.ServiceAccount.LocalSystem; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
run under the system account.&lt;/span&gt; spi.Password &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;null&lt;/span&gt;;
spi.Username &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;null&lt;/span&gt;; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;this&lt;/span&gt;.Installers.Add(spi);
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
After your code is tested and compiled, you can deploy it to a server. Copy to a location
on the server the service EXE and any associated DLLs, Config files or other objects
required for it to run. The server must have the .Net framework installed. If it has
the framework, it should have a program called InstallUtil.exe. You can find this
program in the Windows folder under &lt;em&gt;Microsoft.NET\Framework &lt;/em&gt;in the subfolder
corresponding to the .Net CLR version under which you compiled the service. On my
server, I found InstallUtil.exe in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727.
Open a command prompt, change to the location of InstallUtil.exe and run the following
command&lt;br&gt;
INSTALLUTIL &amp;lt;full path of Windows Service Executable&amp;gt;
&lt;/p&gt;
&lt;p&gt;
You can later uninstall the service with the following command&lt;br&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;INSTALLUTIL
/u &amp;lt;full path of Windows Service Executable&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now open the &lt;em&gt;Services Manager &lt;/em&gt;applet (under Windows &lt;em&gt;Administrative Tools&lt;/em&gt;)
and refresh the list of services. You should see your service listed with the &lt;em&gt;DisplayName &lt;/em&gt;you
assigned in the &lt;em&gt;Installer &lt;/em&gt;class. Right-click this service and select &lt;strong&gt;Properties &lt;/strong&gt;to
display the &lt;strong&gt;Service Properties &lt;/strong&gt;dialog. Click the "&lt;strong&gt;Log On&lt;/strong&gt;"
tab, select "A particular user" and enter the name and password of the user under
which this service should run. You may need to create this user. It should have permission
to access all resources that the service needs to access. Click the &lt;strong&gt;OK &lt;/strong&gt;button
to save these changes and close the dialog.
&lt;/p&gt;
&lt;p&gt;
Of course, you can also write a setup project to your solution if you want to automate
the deployment process. This article does not cover that.
&lt;/p&gt;
&lt;p&gt;
To start the service, right-click the service in the &lt;em&gt;Services Manager &lt;/em&gt;and
select &lt;strong&gt;Start&lt;/strong&gt;.&amp;nbsp; You can use the same applet to stop the service.
Right-click its name and select &lt;strong&gt;Stop&lt;/strong&gt;. Alternatively, you can stop
and start the service from the command line with the following commands
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;NET
START &amp;lt;ServiceName&amp;gt; NET STOP &amp;lt;ServiceName&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
where &amp;lt;ServiceName&amp;gt; s the ServiceInstaller ServiceName property specified in
our installer class. For our example, we would type. This is a useful if you want
to script the starting and stopping of your service.
&lt;/p&gt;
&lt;p&gt;
Windows Services are something that .Net got right very early and hasn't needed to
change. Creating useful services is easy with the .Net framework.
&lt;/p&gt;
&lt;p&gt;
You can download a simple Windows service at &lt;a href="http://www.davidgiard.com/content/binary/DGWinSvc.zip"&gt;DGWinSvc.zip
(28.05 KB)&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=bfbb8a12-54aa-412f-b0bc-45d46285b722" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,bfbb8a12-54aa-412f-b0bc-45d46285b722.aspx</comments>
      <category>.Net</category>
      <category>Back To Basics</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=ec46ef9d-9cae-4629-9cf9-e10c20d795ef</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,ec46ef9d-9cae-4629-9cf9-e10c20d795ef.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,ec46ef9d-9cae-4629-9cf9-e10c20d795ef.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ec46ef9d-9cae-4629-9cf9-e10c20d795ef</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
The <em>yield return</em> and <em>yield break</em> keywords are shortcuts introduced
in C# 3.0. They are designed to assist you in a method that returns IEnumerable &lt;T&gt;.<br />
Often such methods consist of the following steps
</p>
        <ol>
          <li>
Create an empty IEnumerable set. 
</li>
          <li>
Loop through some data (via a <em>while</em>, <em>foreach </em>or other construct) 
</li>
          <li>
Add to the set within your loop. 
</li>
          <li>
Exit the loop when some limit is reached. 
</li>
          <li>
Return the set.</li>
        </ol>
        <p>
A sample of such code is shown below.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> IEnumerable&lt;Int32&gt;
GetOddNumbers_Old(Int32 maxNumber) { List&lt;Int32&gt; oddSet <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> List&lt;Int32&gt;(); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">int</span> num <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 0; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">while</span> (<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">true</span>)
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"> System.Threading.Thread.Sleep(1000);</span> num++; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">if</span> (num <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">%</span> 2
== 1) { oddSet.Add (num); } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">if</span> (num
&gt;= maxNumber) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">break</span>;
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> oddSet;
}</span>
        </pre>
        <p>
The new <em>yield</em> keywords allow you to shorten your code to only the following
steps
</p>
        <ol>
          <li>
Loop through some data 
</li>
          <li>
yield return within the loop 
</li>
          <li>
yield break if a limit is reached before the loop is complete</li>
        </ol>
        <p>
That’s it. There is no need to explicitly create or return your IEnumerable set. 
</p>
        <p>
The <em>yield return</em> statement appends a value to the IEnumerable set. The <em>yield
break</em> statement exits any looping construct to prevent more items being added
to the return set. If you omit <em>yield break</em>, the loop exits as it normally
would.
</p>
        <p>
The difference between using the <em>yield return</em> and simply returning a set
is subtle. Returning a set at the end of the method returns the entire set at
once. The client must wait until the method is complete before it can use any
values in the return set. Each time the <em>yield return</em> statement executes,
the client has access to another element in the set. Essentially the set trickles
back a little at a time. This can provide a gain in performance or perceived performance
if you have a large set to return.
</p>
        <p>
Here is a sample that returns the same results as above, using <em>yield return</em> and <em>yield
break</em>.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> IEnumerable&lt;Int32&gt;
GetOddNumbers_Yield(Int32 maxNumber) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">int</span> num <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 0; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">while</span> (<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">true</span>)
{<br /><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"> System.Threading.Thread.Sleep(1000);</span> num++; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">if</span> (num <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">%</span> 2
== 1) { yield <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> num;
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">if</span> (num
&gt;= maxNumber) { yield <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">break</span>;
} } }</span>
        </pre>
        <p>
If we call the two methods above, we will get the same results but they will return
in slightly different ways. I added the <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">System.Threading.Thread.Sleep(1000); </span>line
to help demonstrate this. That line simply pauses execution for 1 second each time
the loop executes. 
</p>
        <p>
Assume the above two methods are in a class named <em>MyMethods</em>.  The following
code will call each.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Get
Odd Numbers using old way:"</span>); MyMethods mm <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> MyMethods();
IEnumerable&lt;Int32&gt; oddNumbers2 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> mm.GetOddNumbers_Old(11); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">foreach</span> (Int32
n <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">in</span> oddNumbers2)
{ Console.WriteLine(n); } Console.WriteLine(); Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Get
Odd Numbers using new YIELD keyword:"</span>); IEnumerable&lt;Int32&gt; oddNumbers1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> mm.GetOddNumbers_Old(11); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">foreach</span> (Int32
n <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">in</span> oddNumbers1)
{ Console.WriteLine(n); } Console.ReadLine(); </span>
        </pre>
        <p>
The above code produces the following output
</p>
        <blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
          <p>
            <font face="Courier New">Get Odd Numbers using old way:<br />
1<br />
3<br />
5<br />
7<br />
9<br />
11</font>
          </p>
          <p>
            <font face="Courier New">Get Odd Numbers using new YIELD keyword:<br />
1<br />
3<br />
5<br />
7<br />
9<br />
11<br /></font>
          </p>
        </blockquote>
        <p>
Notice that both methods return the same data. However, if you watch the code
as it runs, you will notice the first method waits 12 seconds, then immediately outputs
all odd numbers; while the second method outputs a number every 2 seconds. Each method
returns the same values and each takes the same amount of time, but the second method
uses <em>yield return</em>, so the client is able to output a bit of data
each time a <em>yield return </em>executes.
</p>
        <p>
There are a few restrictions to using the <em>yield </em>keywords 
</p>
        <ul>
          <li>
These keywords cannot appear in blocks marked "unsafe" 
</li>
          <li>
These keywords cannot appear in a method with <em>ref </em>or <em>out</em> parameters 
</li>
          <li>
The <em>yield return </em>statement is not allowed inside a <em>try-catch </em>block,
although it may be located inside a <em>try-finally </em>block. 
</li>
          <li>
A <em>yield break</em> statement may not be located inside a <em>finally</em> block.</li>
        </ul>
        <p>
You can download this sample code at <a href="http://www.davidgiard.com/content/binary/DemoYield.zip">DemoYield.zip
(45.06 KB)</a></p>
        <p>
          <hr />
        </p>
        <p>
        </p>
        <p>
          <a href="http://mvwood.com/" target="_blank">
            <font size="1">Mike Wood</font>
          </a>
          <font size="1"> contributed
to this article.</font>
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=ec46ef9d-9cae-4629-9cf9-e10c20d795ef" />
      </body>
      <title>Yield Return and Yield Break in C#</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,ec46ef9d-9cae-4629-9cf9-e10c20d795ef.aspx</guid>
      <link>http://www.davidgiard.com/2009/09/13/YieldReturnAndYieldBreakInC.aspx</link>
      <pubDate>Sun, 13 Sep 2009 13:44:07 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;em&gt;yield return&lt;/em&gt; and &lt;em&gt;yield break&lt;/em&gt; keywords are shortcuts introduced
in C# 3.0. They are designed to assist you in a method that returns IEnumerable &amp;lt;T&amp;gt;.&lt;br&gt;
Often such methods consist of the following steps
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create an empty IEnumerable set. 
&lt;li&gt;
Loop through some data (via a &lt;em&gt;while&lt;/em&gt;, &lt;em&gt;foreach &lt;/em&gt;or other construct) 
&lt;li&gt;
Add to the set within your loop. 
&lt;li&gt;
Exit the loop when some limit is reached. 
&lt;li&gt;
Return the set.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
A sample of such code is shown below.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Int32&amp;gt;
GetOddNumbers_Old(Int32 maxNumber) { List&amp;lt;Int32&amp;gt; oddSet &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; List&amp;lt;Int32&amp;gt;(); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;int&lt;/span&gt; num &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 0; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;while&lt;/span&gt; (&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;true&lt;/span&gt;)
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; System.Threading.Thread.Sleep(1000);&lt;/span&gt; num++; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;if&lt;/span&gt; (num &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;%&lt;/span&gt; 2
== 1) { oddSet.Add (num); } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;if&lt;/span&gt; (num
&amp;gt;= maxNumber) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;break&lt;/span&gt;;
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; oddSet;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The new &lt;em&gt;yield&lt;/em&gt; keywords allow you to shorten your code to only the following
steps
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Loop through some data 
&lt;li&gt;
yield return within the loop 
&lt;li&gt;
yield break if a limit is reached before the loop is complete&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
That’s it. There is no need to explicitly create or return your IEnumerable set. 
&lt;/p&gt;
&lt;p&gt;
The &lt;em&gt;yield return&lt;/em&gt; statement appends a value to the IEnumerable set. The &lt;em&gt;yield
break&lt;/em&gt; statement exits any looping construct to prevent more items being added
to the return set. If you omit &lt;em&gt;yield break&lt;/em&gt;, the loop exits as it normally
would.
&lt;/p&gt;
&lt;p&gt;
The difference between using the &lt;em&gt;yield return&lt;/em&gt; and simply returning a set
is subtle. Returning a set at the end of the method returns&amp;nbsp;the entire set at
once.&amp;nbsp;The client must wait until the method is complete before it can use any
values in the return set. Each time the &lt;em&gt;yield return&lt;/em&gt; statement&amp;nbsp;executes,
the client has access to another element in the set. Essentially the set trickles
back a little at a time. This can provide a gain in performance or perceived performance
if you have a large set to return.
&lt;/p&gt;
&lt;p&gt;
Here is a sample that returns the same results as above, using &lt;em&gt;yield return&lt;/em&gt; and &lt;em&gt;yield
break&lt;/em&gt;.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Int32&amp;gt;
GetOddNumbers_Yield(Int32 maxNumber) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;int&lt;/span&gt; num &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 0; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;while&lt;/span&gt; (&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;true&lt;/span&gt;)
{&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; System.Threading.Thread.Sleep(1000);&lt;/span&gt; num++; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;if&lt;/span&gt; (num &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;%&lt;/span&gt; 2
== 1) { yield &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; num;
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;if&lt;/span&gt; (num
&amp;gt;= maxNumber) { yield &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;break&lt;/span&gt;;
} } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
If we call the two methods above, we will get the same results but they will return
in slightly different ways. I added the&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;System.Threading.Thread.Sleep(1000); &lt;/span&gt;line
to help demonstrate this. That line simply pauses execution for 1 second each time
the loop executes. 
&lt;/p&gt;
&lt;p&gt;
Assume the above two methods are in a class named &lt;em&gt;MyMethods&lt;/em&gt;.&amp;nbsp; The following
code will call each.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Get
Odd Numbers using old way:"&lt;/span&gt;); MyMethods mm &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; MyMethods();
IEnumerable&amp;lt;Int32&amp;gt; oddNumbers2 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; mm.GetOddNumbers_Old(11); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;foreach&lt;/span&gt; (Int32
n &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;in&lt;/span&gt; oddNumbers2)
{ Console.WriteLine(n); } Console.WriteLine(); Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Get
Odd Numbers using new YIELD keyword:"&lt;/span&gt;); IEnumerable&amp;lt;Int32&amp;gt; oddNumbers1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; mm.GetOddNumbers_Old(11); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;foreach&lt;/span&gt; (Int32
n &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;in&lt;/span&gt; oddNumbers1)
{ Console.WriteLine(n); } Console.ReadLine(); &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The above code produces the following output
&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir=ltr&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;Get Odd Numbers using old way:&lt;br&gt;
1&lt;br&gt;
3&lt;br&gt;
5&lt;br&gt;
7&lt;br&gt;
9&lt;br&gt;
11&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;Get Odd Numbers using new YIELD keyword:&lt;br&gt;
1&lt;br&gt;
3&lt;br&gt;
5&lt;br&gt;
7&lt;br&gt;
9&lt;br&gt;
11&lt;br&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Notice that both methods return the same data.&amp;nbsp;However, if you watch the code
as it runs, you will notice the first method waits 12 seconds, then immediately outputs
all odd numbers; while the second method outputs a number every 2 seconds. Each method
returns the same values and each takes the same amount of time, but the second method
uses &lt;em&gt;yield return&lt;/em&gt;, so the client is able to output&amp;nbsp;a bit of&amp;nbsp;data
each time a &lt;em&gt;yield return &lt;/em&gt;executes.
&lt;/p&gt;
&lt;p&gt;
There are a few restrictions to using the &lt;em&gt;yield &lt;/em&gt;keywords 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
These keywords cannot appear in blocks marked "unsafe" 
&lt;li&gt;
These keywords cannot appear in a method with &lt;em&gt;ref &lt;/em&gt;or &lt;em&gt;out&lt;/em&gt; parameters 
&lt;li&gt;
The &lt;em&gt;yield return &lt;/em&gt;statement is not allowed inside a &lt;em&gt;try-catch &lt;/em&gt;block,
although it may be located inside a &lt;em&gt;try-finally &lt;/em&gt;block. 
&lt;li&gt;
A &lt;em&gt;yield break&lt;/em&gt; statement may not be located inside a &lt;em&gt;finally&lt;/em&gt; block.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
You can download this sample code at &lt;a href="http://www.davidgiard.com/content/binary/DemoYield.zip"&gt;DemoYield.zip
(45.06 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;hr&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://mvwood.com/" target=_blank&gt;&lt;font size=1&gt;Mike Wood&lt;/font&gt;&lt;/a&gt;&lt;font size=1&gt; contributed
to this article.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=ec46ef9d-9cae-4629-9cf9-e10c20d795ef" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,ec46ef9d-9cae-4629-9cf9-e10c20d795ef.aspx</comments>
      <category>Back To Basics</category>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=3e959a7b-8546-477c-b606-324c1671e5e9</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,3e959a7b-8546-477c-b606-324c1671e5e9.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,3e959a7b-8546-477c-b606-324c1671e5e9.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=3e959a7b-8546-477c-b606-324c1671e5e9</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
Prior to C# 3.0, we had two common ways to initialize the public properties of an
object. 
</p>
        <ol>
          <li>
We could use the default constructor to instantiate the object; then, use a separate
line of code to assign a value to each property. (Listing 1) 
</li>
          <li>
We could create a constructor that accepts an argument for each property; and write
code in this constructor to assign to each public property the value passed in for
each corresponding argument. (Listing 2 and Listing 3)</li>
        </ol>
        <p>
The first method is quite verbose and requires a lot of code for complex objects. 
</p>
        <p>
          <strong>Listing 1:</strong>
        </p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">Customer
cust1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Customer();
cust1.CustomerID <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 1;
cust1.FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"George"</span>;
cust1.LastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Washington"</span>;
cust1.StreetAddress <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"111
A St"</span>; cust1.City <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Aville"</span>;
cust1.State <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"MI"</span>;
cust1.ZipCode <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"10001"</span>;</span>
        </pre>
        <p>
          <strong>Listing 2:</strong>
        </p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Customer
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> Int32
CustomerID { get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> LastName
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> StreetAddress
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> City
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> State
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> ZipCode
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> Customer
( Int32 customerID, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> firstName, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> lastName, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> streetAddress, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> city, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> state, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> zipCode
) { CustomerID <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> customerID;
FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> firstName;
LastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> lastName;
StreetAddress <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> streetAddress;
City <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> city;
State <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> state;
ZipCode <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> zipCode;
} } </span>
        </pre>
        <p>
          <strong>Listing 3:</strong>
        </p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">Customer
cust2 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Customer
( 2, <span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"John"</span>, <span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Adams"</span>, <span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"222
B Ave"</span>, <span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Beetown"</span>, <span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"MI"</span>, <span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"20002"</span> ); </span>
        </pre>
        <p>
The second method is more elegant, but assumes we always want to initialize every
single public property at object instantiation. Of course, we could create multiple
constructors to accommodate times when we only want to initialize certain variables
but this can get complex in a hurry when our class has a lot of public properties.
</p>
        <p>
Beginning with C# 3.0, we can now initialize public properties in the same line of
that instantiates an object; and we can do so without creating a new constructor.
We use object initializers to do this.  
</p>
        <p>
Using object initializers we can still use the default constructor (or any public
constructor we want); but we can set public properties by appending the property assignments
within a set of curly braces at the end of the object initializer, as shown in listing
4.
</p>
        <p>
          <strong>Listing 4:</strong>
        </p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">Customer
cust4 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Customer()
{ CustomerID <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 4,
FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"James"</span>,
LastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Monroe"</span> }; </span>
        </pre>
        <p>
This new C# feature is known as <em>object initializers</em>. This method gives us
a great deal of flexibility. At the time we instantiate an object, we can decide which
properties to initialize. And we can do so without creating a plethora of constructors
for the object or return to the verbose way of assigning property values after an
object's instantiation. 
</p>
        <p>
Object initializers are most useful when you instantiate a class in many different
places throughtout an application.
</p>
Download this code to view and run a simple application that uses object initializers <a href="http://www.davidgiard.com/content/binary/DemoObjectInitializers.zip">DemoObjectInitializers.zip
(24.92 KB)</a><img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=3e959a7b-8546-477c-b606-324c1671e5e9" /></body>
      <title>Object Initializers in C#</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,3e959a7b-8546-477c-b606-324c1671e5e9.aspx</guid>
      <link>http://www.davidgiard.com/2009/09/12/ObjectInitializersInC.aspx</link>
      <pubDate>Sat, 12 Sep 2009 13:10:03 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt;
&lt;/p&gt;
&lt;p&gt;
Prior to C# 3.0, we had two common ways to initialize the public properties of an
object. 
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
We could use the default constructor to instantiate the object; then, use a separate
line of code to assign a value to each property. (Listing 1) 
&lt;li&gt;
We could create a constructor that accepts an argument for each property; and write
code in this constructor to assign to each public property the value passed in for
each corresponding argument. (Listing 2 and Listing 3)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The first method is quite verbose and requires a lot of code for complex objects. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Listing 1:&lt;/strong&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;Customer
cust1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Customer();
cust1.CustomerID &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 1;
cust1.FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"George"&lt;/span&gt;;
cust1.LastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Washington"&lt;/span&gt;;
cust1.StreetAddress &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"111
A St"&lt;/span&gt;; cust1.City &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Aville"&lt;/span&gt;;
cust1.State &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"MI"&lt;/span&gt;;
cust1.ZipCode &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"10001"&lt;/span&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;Listing 2:&lt;/strong&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Customer
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; Int32
CustomerID { get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; LastName
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; StreetAddress
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; City
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; State
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; ZipCode
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; Customer
( Int32 customerID, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; firstName, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; lastName, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; streetAddress, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; city, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; state, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; zipCode
) { CustomerID &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; customerID;
FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; firstName;
LastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; lastName;
StreetAddress &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; streetAddress;
City &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; city;
State &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; state;
ZipCode &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; zipCode;
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;Listing 3:&lt;/strong&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;Customer
cust2 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Customer
( 2, &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"John"&lt;/span&gt;, &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Adams"&lt;/span&gt;, &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"222
B Ave"&lt;/span&gt;, &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Beetown"&lt;/span&gt;, &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"MI"&lt;/span&gt;, &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"20002"&lt;/span&gt; ); &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The second method is more elegant, but assumes we always want to initialize every
single public property at object instantiation. Of course, we could create multiple
constructors to accommodate times when we only want to initialize certain variables
but this can get complex in a hurry when our class has a lot of public properties.
&lt;/p&gt;
&lt;p&gt;
Beginning with C# 3.0, we can now initialize public properties in the same line of
that instantiates an object; and we can do so without creating a new constructor.
We use object initializers to do this.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Using object initializers we can still use the default constructor (or any public
constructor we want); but we can set public properties by appending the property assignments
within a set of curly braces at the end of the object initializer, as shown in listing
4.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Listing 4:&lt;/strong&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;Customer
cust4 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Customer()
{ CustomerID &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 4,
FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"James"&lt;/span&gt;,
LastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Monroe"&lt;/span&gt; }; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
This new C# feature is known as &lt;em&gt;object initializers&lt;/em&gt;. This method gives us
a great deal of flexibility. At the time we instantiate an object, we can decide which
properties to initialize. And we can do so without creating a plethora of constructors
for the object or return to the verbose way of assigning property values after an
object's instantiation. 
&lt;/p&gt;
&lt;p&gt;
Object initializers are most useful when you instantiate a class in many different
places throughtout an application.
&lt;/p&gt;
Download this code to view and run a simple application that uses object initializers &lt;a href="http://www.davidgiard.com/content/binary/DemoObjectInitializers.zip"&gt;DemoObjectInitializers.zip
(24.92 KB)&lt;/a&gt;&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=3e959a7b-8546-477c-b606-324c1671e5e9" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,3e959a7b-8546-477c-b606-324c1671e5e9.aspx</comments>
      <category>Back To Basics</category>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=d0c8a032-d0f7-49e8-ae4f-959b536aed1b</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,d0c8a032-d0f7-49e8-ae4f-959b536aed1b.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,d0c8a032-d0f7-49e8-ae4f-959b536aed1b.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=d0c8a032-d0f7-49e8-ae4f-959b536aed1b</wfw:commentRss>
      <slash:comments>15</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
Automatic properties were introduced in C# 3.0 as a convenience to developers.
</p>
        <p>
C# and Visual Basic .Net provide two ways for a class to maintain state: Fields and
Properties. 
</p>
        <p>
A field is a simply a public variable defined inside a class, such as in the following
example
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> foo;</span>
        </pre>
        <p>
A property looks much like a field to the outside world, but exposes two methods –
a “getter” to allow you to retrieve the value of the property and a “setter” to allow
you to assign a value to the property. The following sample demonstrates how to implement
a read-write property in C#.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _firstName; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _firstName;
} set { _firstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} }</span>
        </pre>
        <p>
This is a common use of property getters and setters – to assign and retrieve the
value from a private variable. Because there is no extra logic in these methods, similar
functionality could be achieved using a field, but I prefer to use properties in case
I decide later to add some validations or calculations to the setter or getter. Making
a change like that to a property will not break the class signature, forcing a recompile
of dependent objects. 
</p>
        <p>
But this is a lot of typing for such simple functionality. So much in fact that Visual
Studio 2005 provided a right-click accelerator to type much of it for you.
</p>
        <p>
Beginning with C# 3.0, automatic properties provide a shorthand notation for this
functionality.  Simply provide the scope, the type and the name of the property,
followed by “{get; set; }” and the framework will automatically create a private variable
for you and provide code to save and retrieve to/from that private variable. This
is all done behind the scenes for us so we don’t need to write any of this code. 
Below is a sample of the code we need to write for a public property.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get; set; }</span>
        </pre>
        <p>
To make a property read-only, simply omit the "set" keyword. To make it write-only,
omit the "get" keyword. Below is syntax for the same property if I wanted it to be
read-only.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get; }</span>
        </pre>
        <p>
The only disadvantage to automatic properties is that the rest of the code in our
class no longer has access to the private variables create by the framework. Even
from within the same class, we must go through the public property in order to get
or set the private variable. 
</p>
        <p>
This is a small price to pay for the amount of code we are saving. 
</p>
        <p>
Automatic properties not only require less typing, they are far easier to read when
your class has a bunch of properties. Which of the following easier to read?
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> LastName
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> StreetAddress
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> City
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> State
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Zip
{ get; set; }</span>
        </pre>
        <p>
or 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _firstName; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _firstName;
} set { _firstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _lastName; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> LastName
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _lastName;
} set { _lastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _streetAddress; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> StreetAddress
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _streetAddress;
} set { _streetAddress <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _city; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> City
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _city;
} set { _city <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _state; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> State
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _state;
} set { _state <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _zip; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Zip
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _zip;
} set { _zip <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} } </span>
        </pre>
        <p>
I think you’ll agree that it is far easier to read and understand the more terse syntax
of the first example.  And code that is easier to understand is easier to maintain. 
</p>
        <p>
Because both syntax versions compile to the same Intermediate Language, I recommend
always using the newer Automatic Properties syntax when you create properties that
do nothing more than save and retrieve state.
</p>
Download the following sample code to see automatic properties in action: <a href="http://www.davidgiard.com/content/binary/DemoAutomaticProperties.zip">DemoAutomaticProperties.zip
(26.83 KB)</a><img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=d0c8a032-d0f7-49e8-ae4f-959b536aed1b" /></body>
      <title>Automatic Properties in C#</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,d0c8a032-d0f7-49e8-ae4f-959b536aed1b.aspx</guid>
      <link>http://www.davidgiard.com/2009/09/11/AutomaticPropertiesInC.aspx</link>
      <pubDate>Fri, 11 Sep 2009 12:17:17 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt;
&lt;/p&gt;
&lt;p&gt;
Automatic properties were introduced in C# 3.0 as a convenience to developers.
&lt;/p&gt;
&lt;p&gt;
C# and Visual Basic .Net provide two ways for a class to maintain state: Fields and
Properties. 
&lt;/p&gt;
&lt;p&gt;
A field is a simply a public variable defined inside a class, such as in the following
example
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; foo;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
A property looks much like a field to the outside world, but exposes two methods –
a “getter” to allow you to retrieve the value of the property and a “setter” to allow
you to assign a value to the property. The following sample demonstrates how to implement
a read-write property in C#.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _firstName; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _firstName;
} set { _firstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
This is a common use of property getters and setters – to assign and retrieve the
value from a private variable. Because there is no extra logic in these methods, similar
functionality could be achieved using a field, but I prefer to use properties in case
I decide later to add some validations or calculations to the setter or getter. Making
a change like that to a property will not break the class signature, forcing a recompile
of dependent objects. 
&lt;/p&gt;
&lt;p&gt;
But this is a lot of typing for such simple functionality. So much in fact that Visual
Studio 2005 provided a right-click accelerator to type much of it for you.
&lt;/p&gt;
&lt;p&gt;
Beginning with C# 3.0, automatic properties provide a shorthand notation for this
functionality.&amp;nbsp; Simply provide the scope, the type and the name of the property,
followed by “{get; set; }” and the framework will automatically create a private variable
for you and provide code to save and retrieve to/from that private variable. This
is all done behind the scenes for us so we don’t need to write any of this code.&amp;nbsp;
Below is a sample of the code we need to write for a public property.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get; set; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
To make a property read-only, simply omit the "set" keyword. To make it write-only,
omit the "get" keyword. Below is syntax for the same property if I wanted it to be
read-only.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The only disadvantage to automatic properties is that the rest of the code in our
class no longer has access to the private variables create by the framework. Even
from within the same class, we must go through the public property in order to get
or set the private variable. 
&lt;/p&gt;
&lt;p&gt;
This is a small price to pay for the amount of code we are saving. 
&lt;/p&gt;
&lt;p&gt;
Automatic properties not only require less typing, they are far easier to read when
your class has a bunch of properties. Which of the following easier to read?
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; LastName
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; StreetAddress
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; City
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; State
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Zip
{ get; set; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
or 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _firstName; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _firstName;
} set { _firstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _lastName; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; LastName
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _lastName;
} set { _lastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _streetAddress; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; StreetAddress
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _streetAddress;
} set { _streetAddress &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _city; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; City
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _city;
} set { _city &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _state; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; State
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _state;
} set { _state &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _zip; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Zip
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _zip;
} set { _zip &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
I think you’ll agree that it is far easier to read and understand the more terse syntax
of the first example.&amp;nbsp; And code that is easier to understand is easier to maintain. 
&lt;/p&gt;
&lt;p&gt;
Because both syntax versions compile to the same Intermediate Language, I recommend
always using the newer Automatic Properties syntax when you create properties that
do nothing more than save and retrieve state.
&lt;/p&gt;
Download the following sample code to see automatic properties in action: &lt;a href="http://www.davidgiard.com/content/binary/DemoAutomaticProperties.zip"&gt;DemoAutomaticProperties.zip
(26.83 KB)&lt;/a&gt;&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=d0c8a032-d0f7-49e8-ae4f-959b536aed1b" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,d0c8a032-d0f7-49e8-ae4f-959b536aed1b.aspx</comments>
      <category>Back To Basics</category>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=2093fc2d-091e-4201-98a0-6bec65cb2f3d</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,2093fc2d-091e-4201-98a0-6bec65cb2f3d.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,2093fc2d-091e-4201-98a0-6bec65cb2f3d.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=2093fc2d-091e-4201-98a0-6bec65cb2f3d</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
Extensions methods are a new feature of C# 3.0 and they are easier to use than they
first appear.
</p>
        <p>
An extension method is a method that is external to an existing class but appears
as if it were a method on that class. 
</p>
        <p>
The rules for creating an extension method are simple.
</p>
        <ol>
          <li>
Create a static method 
</li>
          <li>
The first parameter of the static method should be the type of the class you wish
to extend 
</li>
          <li>
Precede the parameter type of this first parameter with the "this" keyword. 
</li>
          <li>
Call the method as if it were a method of the class. Omit the first parameter.</li>
        </ol>
        <p>
An example should clarify this. Assume we have a class <em>Customer</em> with properties <em>FirstName </em>and <em>LastName </em>as
shown below
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Customer
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> LastName
{ get; set; } } </span>
        </pre>
        <p>
We can create a new static class <em>MyExtensions </em>with a static method <em>GetFullName </em>that
returns the formatted first and last name of the customer. We do so with the following
code
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> MyExtensions
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> GetFullName(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">this</span> Customer
cust) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> custName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"
"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> cust.LastName; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> custName.Trim();
} } </span>
        </pre>
        <p>
Notice the parameter with the "this" keyword. That parameter format tells the compiler
that this is an extension method and that it should extend the <em>Customer </em>class.
As long as <em>MyExtensions</em> is in the same namespace or in a namespace available
to our code (via the "using" statement), we can call this new extension method with
the following code
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">Customer
cust <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Customer
{ FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"David"</span>,
LastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Giard"</span> }; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> fName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.GetFullName();
Console.WriteLine(fName); </span>
        </pre>
        <p>
The code above outputs:
</p>
        <p>
   <font face="Courier New">David Giard</font></p>
        <p>
As you can see in the above code, it looks as if the <em>GetFullName </em>method is
part of the <em>Customer </em>class.
</p>
        <p>
We can add parameters to our extension methods as we would to any other method. The
first parameter (with the “this” keyword) is always used to specify the class we are
extending. All other parameters act just like normal parameters. The following extension
method accepts a parameter “salutation”.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> GetGreeting(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">this</span> Customer
cust, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> salutation)
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> custName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"
"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> cust.LastName;
custName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> custName.Trim(); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> salutation <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> “
“ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> custName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">":"</span>;
} </span>
        </pre>
        <p>
Although the extension method has two parameters, we only need to pass the second
parameter when calling it, as shown
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">Customer
cust <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Customer
{ FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"David"</span>,
LastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Giard"</span> }; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> greeting <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.GetGreeting(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Dear"</span>);
Console.WriteLine(greeting); </span>
        </pre>
        <p>
The code above outputs:
</p>
        <p>
   Dear <font face="Courier New">David Giard:</font></p>
        <p>
In our examples, we were adding extension methods to a class that we just created.
Of course, in this case, it would have been simpler to just modify the original class. 
But extension methods are more useful if you are working with someone else’s class
and modifying the source code is not an option. Extension methods often offer a simpler
solution than inheriting from an existing class.
</p>
        <p>
The real power of extension methods comes from the fact that you can even add methods
to sealed classes. It is difficult to add functionality to a sealed class because
we cannot inherit from it. Change the Customer class to sealed and re-run the code
to prove that it still works.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">sealed</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Customer </span>
        </pre>
        <p>
Here is the all code in the above sample
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">using</span> System; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">namespace</span> TestExtensionMethods
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span>[]
args) { Customer cust <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Customer
{ FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"David"</span>,
LastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Giard"</span> }; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> fn <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.GetFullName();
Console.WriteLine(fn); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> greeting <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.GetGreeting(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Dear"</span>);
Console.WriteLine(greeting); Console.ReadLine(); } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">sealed</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Customer
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> LastName
{ get; set; } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> MyExtensions
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> GetFullName(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">this</span> Customer
cust) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> n <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"
"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> cust.LastName; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> n.Trim();
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> GetGreeting(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">this</span> Customer
cust, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> salutation)
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> custName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cust.FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"
"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> cust.LastName;
custName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> custName.Trim(); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> salutation <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"
"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> custName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">":"</span>;
} } }</span>
        </pre>
        <p>
You can download the sample code at <a href="http://www.davidgiard.com/content/binary/TestExtensionMethods.zip">TestExtensionMethods.zip
(24.26 KB)</a></p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=2093fc2d-091e-4201-98a0-6bec65cb2f3d" />
      </body>
      <title>Extensions Methods in C#</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,2093fc2d-091e-4201-98a0-6bec65cb2f3d.aspx</guid>
      <link>http://www.davidgiard.com/2009/09/05/ExtensionsMethodsInC.aspx</link>
      <pubDate>Sat, 05 Sep 2009 01:52:43 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt;
&lt;/p&gt;
&lt;p&gt;
Extensions methods are a new feature of C# 3.0 and they are easier to use than they
first appear.
&lt;/p&gt;
&lt;p&gt;
An extension method is a method that is external to an existing class but appears
as if it were a method on that class. 
&lt;/p&gt;
&lt;p&gt;
The rules for creating an extension method are simple.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Create a static method 
&lt;li&gt;
The first parameter of the static method should be the type of the class you wish
to extend 
&lt;li&gt;
Precede the parameter type of this first parameter with the "this" keyword. 
&lt;li&gt;
Call the method as if it were a method of the class. Omit the first parameter.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
An example should clarify this. Assume we have a class &lt;em&gt;Customer&lt;/em&gt; with properties &lt;em&gt;FirstName &lt;/em&gt;and &lt;em&gt;LastName &lt;/em&gt;as
shown below
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Customer
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; LastName
{ get; set; } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We can create a new static class &lt;em&gt;MyExtensions &lt;/em&gt;with a static method &lt;em&gt;GetFullName &lt;/em&gt;that
returns the formatted first and last name of the customer. We do so with the following
code
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; MyExtensions
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; GetFullName(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;this&lt;/span&gt; Customer
cust) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; custName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"
"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; cust.LastName; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; custName.Trim();
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Notice the parameter with the "this" keyword. That parameter format tells the compiler
that this is an extension method and that it should extend the &lt;em&gt;Customer &lt;/em&gt;class.
As long as &lt;em&gt;MyExtensions&lt;/em&gt; is in the same namespace or in a namespace available
to our code (via the "using" statement), we can call this new extension method with
the following code
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;Customer
cust &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Customer
{ FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"David"&lt;/span&gt;,
LastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Giard"&lt;/span&gt; }; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; fName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.GetFullName();
Console.WriteLine(fName); &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The code above outputs:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font face="Courier New"&gt;David Giard&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
As you can see in the above code, it looks as if the &lt;em&gt;GetFullName &lt;/em&gt;method is
part of the &lt;em&gt;Customer &lt;/em&gt;class.
&lt;/p&gt;
&lt;p&gt;
We can add parameters to our extension methods as we would to any other method. The
first parameter (with the “this” keyword) is always used to specify the class we are
extending. All other parameters act just like normal parameters. The following extension
method accepts a parameter “salutation”.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; GetGreeting(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;this&lt;/span&gt; Customer
cust, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; salutation)
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; custName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"
"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; cust.LastName;
custName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; custName.Trim(); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; salutation &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; “
“ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; custName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;":"&lt;/span&gt;;
} &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Although the extension method has two parameters, we only need to pass the second
parameter when calling it, as shown
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;Customer
cust &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Customer
{ FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"David"&lt;/span&gt;,
LastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Giard"&lt;/span&gt; }; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; greeting &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.GetGreeting(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Dear"&lt;/span&gt;);
Console.WriteLine(greeting); &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The code above outputs:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Dear &lt;font face="Courier New"&gt;David Giard:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
In our examples, we were adding extension methods to a class that we just created.
Of course, in this case, it would have been simpler to just modify the original class.&amp;nbsp;
But extension methods are more useful if you are working with someone else’s class
and modifying the source code is not an option. Extension methods often offer a simpler
solution than inheriting from an existing class.
&lt;/p&gt;
&lt;p&gt;
The real power of extension methods comes from the fact that you can even add methods
to sealed classes. It is difficult to add functionality to a sealed class because
we cannot inherit from it. Change the Customer class to sealed and re-run the code
to prove that it still works.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;sealed&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Customer &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Here is the all code in the above sample
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;using&lt;/span&gt; System; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;namespace&lt;/span&gt; TestExtensionMethods
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt;[]
args) { Customer cust &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Customer
{ FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"David"&lt;/span&gt;,
LastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Giard"&lt;/span&gt; }; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; fn &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.GetFullName();
Console.WriteLine(fn); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; greeting &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.GetGreeting(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Dear"&lt;/span&gt;);
Console.WriteLine(greeting); Console.ReadLine(); } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;sealed&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Customer
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; LastName
{ get; set; } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; MyExtensions
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; GetFullName(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;this&lt;/span&gt; Customer
cust) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; n &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"
"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; cust.LastName; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; n.Trim();
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; GetGreeting(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;this&lt;/span&gt; Customer
cust, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; salutation)
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; custName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cust.FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"
"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; cust.LastName;
custName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; custName.Trim(); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; salutation &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"
"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; custName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;":"&lt;/span&gt;;
} } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
You can download the sample code at &lt;a href="http://www.davidgiard.com/content/binary/TestExtensionMethods.zip"&gt;TestExtensionMethods.zip
(24.26 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=2093fc2d-091e-4201-98a0-6bec65cb2f3d" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,2093fc2d-091e-4201-98a0-6bec65cb2f3d.aspx</comments>
      <category>.Net</category>
      <category>Back To Basics</category>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=0a4c1c0e-22d0-42ce-ae40-86ce986d0139</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,0a4c1c0e-22d0-42ce-ae40-86ce986d0139.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,0a4c1c0e-22d0-42ce-ae40-86ce986d0139.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=0a4c1c0e-22d0-42ce-ae40-86ce986d0139</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
This series of articles provides an introduction to relational databases in general
and Microsoft SQL Server in particular.
</p>
        <p>
          <a href="http://www.davidgiard.com/2009/02/24/SQL101Part1WhatIsADatabase.aspx">Part
1: What is a Database?</a>
        </p>
        <p>
          <a href="http://www.davidgiard.com/2009/02/26/SQL101Part2Relationships.aspx">Part
2: Relationships</a>
        </p>
        <p>
          <a href="http://www.davidgiard.com/2009/03/10/SQL101Part3SelectingDataFromATable.aspx">Part
3: Selecting data from a table</a>
        </p>
        <p>
          <a href="http://www.davidgiard.com/2009/03/12/SQL101Part4AggregatingData.aspx">Part
4: Aggregating data</a>
        </p>
        <p>
          <a href="http://www.davidgiard.com/2009/03/21/SQL101Part5Joins.aspx">Part 5: Joins</a>
        </p>
        <p>
          <a href="http://www.davidgiard.com/2009/03/27/SQL101Part6InsertsUpdatesAndDeletes.aspx">Part
6: Inserts, Updates and Deletes</a>
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=0a4c1c0e-22d0-42ce-ae40-86ce986d0139" />
      </body>
      <title>SQL 101</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,0a4c1c0e-22d0-42ce-ae40-86ce986d0139.aspx</guid>
      <link>http://www.davidgiard.com/2009/08/31/SQL101.aspx</link>
      <pubDate>Mon, 31 Aug 2009 12:04:07 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt;
&lt;/p&gt;
&lt;p&gt;
This series of articles provides an introduction to relational databases in general
and Microsoft SQL Server in particular.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/02/24/SQL101Part1WhatIsADatabase.aspx"&gt;Part
1: What is a Database?&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/02/26/SQL101Part2Relationships.aspx"&gt;Part
2: Relationships&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/03/10/SQL101Part3SelectingDataFromATable.aspx"&gt;Part
3: Selecting data from a table&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/03/12/SQL101Part4AggregatingData.aspx"&gt;Part
4: Aggregating data&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/03/21/SQL101Part5Joins.aspx"&gt;Part 5: Joins&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/03/27/SQL101Part6InsertsUpdatesAndDeletes.aspx"&gt;Part
6: Inserts, Updates and Deletes&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=0a4c1c0e-22d0-42ce-ae40-86ce986d0139" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,0a4c1c0e-22d0-42ce-ae40-86ce986d0139.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
This series of articles provides an introduction to the concepts used in Object Oriented
Prorgramming and OOP code samples in C#.
</p>
        <p>
          <a href="http://www.davidgiard.com/2009/08/04/IntroToOOPPart1WhatIsOOP.aspx">Part
1: What is OOP?</a>
        </p>
        <p>
          <a href="http://www.davidgiard.com/2009/08/05/IntroToOOPPart2UnderstandingObjects.aspx">Part
2: Understanding Objects</a>
        </p>
        <p>
          <a href="http://www.davidgiard.com/2009/08/06/IntroToOOPPart3OOPConcepts.aspx">Part
3: Object Oriented concepts</a>
        </p>
        <p>
 
</p>
        <p>
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d" />
      </body>
      <title>Introduction to Object Oriented Programming</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d.aspx</guid>
      <link>http://www.davidgiard.com/2009/08/07/IntroductionToObjectOrientedProgramming.aspx</link>
      <pubDate>Fri, 07 Aug 2009 02:15:45 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;/p&gt;
&lt;p&gt;
This series of articles provides an introduction to the concepts used in Object Oriented
Prorgramming and OOP code samples in C#.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/08/04/IntroToOOPPart1WhatIsOOP.aspx"&gt;Part
1: What is OOP?&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/08/05/IntroToOOPPart2UnderstandingObjects.aspx"&gt;Part
2: Understanding Objects&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.davidgiard.com/2009/08/06/IntroToOOPPart3OOPConcepts.aspx"&gt;Part
3: Object Oriented concepts&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,e8e40ffd-5d6b-49e3-86e9-3d4f1d6e291d.aspx</comments>
      <category>Back To Basics</category>
      <category>OOP</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=6595467f-c388-403b-832a-6b274dd39e2f</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,6595467f-c388-403b-832a-6b274dd39e2f.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,6595467f-c388-403b-832a-6b274dd39e2f.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=6595467f-c388-403b-832a-6b274dd39e2f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <p>
In this section, we will discuss the key concepts important to Object Oriented Programming
(OOP). An object-oriented system has the following characteristics 
</p>
        <ul>
          <li>
Inheritance 
</li>
          <li>
Polymorphism 
</li>
          <li>
Abstraction 
</li>
          <li>
Encapsulation 
</li>
          <li>
Decoupling 
</li>
        </ul>
        <p>
Some systems (and some languages) don’t fully support all the above constructs and
still refer to themselves as “object-oriented”. This is a matter of some debate, but
it is my opinion that a language or system must implement each of the above concepts
in some way in order to be considered object oriented. 
</p>
        <h3>Inheritance 
</h3>
        <p>
Inheritance is the ability to create a class based on an existing class. In this model,
the existing class is known as the “parent class”, or “base class” and the new class
is known as the “child class” or “derived class”. By default, a child class will inherit
all properties and methods of its parent class. 
</p>
        <p>
In C#, we inherit a class from a parent class by adding appending a colon and the
name of the parent class to the child class definition, as in the following example: 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> ChildClass
: ParentClass {} </span>
        </pre>
        <p>
Marking a method as virtual in a parent class allows it to be overridden in a child
class. This means that the method can be replaced in the child class with new code. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">virtual</span> Int32
DoMath (Int32 num1, Int32 num2) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"> return</span> num1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> num2;
} </span>
        </pre>
        <p>
We can then use the “override” keyword in the child class’s method to replace the
code in the parent’s class method, as shown below 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">override</span> Int32
DoMath (Int32 num1, Int32 num2) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"> return</span> num1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">-</span> num2;
} </span>
        </pre>
        <p>
It is possible to have multiple child classes that inherit from the same parent class.
In some languages (but not in C#), it is also possible for a child class to inherit
from multiple parent classes. 
</p>
        <p>
As a general rule, if I find myself writing a lot of conditional logic in a class’s
methods – depending on how the object is created or the environment, the class runs
one of several blocks of code – I will consider refactoring that class into a number
of subclasses. Each class will inherit from a common parent class but the conditional
logic is eliminated because each subclass contains only the code relevant to how it
is created. 
</p>
        <h3>Interface Inheritance 
</h3>
        <p>
Inheriting from an interface is similar to inheriting from a class, except the parent
class does not contain any implementation, so each subclass contains all method code
it needs to run. This is useful if a set of subclasses need to share the same public
members but do not have any code in common. Interfaces also have the advantage that
a single class can inherit from more than one interface. 
</p>
        <h3>Polymorphism 
</h3>
        <p>
Earlier, we said that objects communicate by passing messages via their public interface. 
</p>
        <p>
Polymorphism is the ability for different objects to respond to the same messages
in a different, but appropriate, way. In OOP, this is done using inheritance. Because
a child class inherits properties, fields and methods from a parent class, we can
have several different child classes sharing the same public members. Each of these
classes would therefore accept the same message types. However, each base class may
override some of the behavior of the base class and may do so in a way appropriate
to itself. 
</p>
        <p>
For example, we may create a Vehicle class that has a method Drive() that accepts
no parameters. 
</p>
        <p>
We can derive two child classes - Car and Airplane - from Vehicle and they will each
inherit the Drive method. But driving a car is not like driving an airplane, so each
of these methods would be overridden and the overridden child methods would be very
different from one another. Calling the Car’s Drive method would cause the axels to
turn and rotate the four wheels beneath the car. Calling the Airplane’s Drive method
would explode jet fuel and propel the airplane through the sky at a high velocity. 
</p>
        <p>
This is an example of two objects (Car and Airplane) that accept the same message
(Drive) and respond differently but appropriately. 
</p>
        <p>
The C# code for this set of classes would look similar to the following 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Vehicle
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"> public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">virtual</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Drive()
{ Console.WriteLine(“Driving…”); } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Automobile:
Vehicle { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"> public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">override</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Drive()
{ Console.WriteLine(“Turn axel and wheels and tires…”); } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Airplane:
Vehicle { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"> public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">override</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Drive()
{ Console.WriteLine(“Explode jet fuel and propel through the air…”); } } </span>
        </pre>
        <p>
The picture below shows the hierarchy of classes that inherit from a single parent
class.<img border="0" src="http://www.davidgiard.com/content/binary/OOPInheritanceClassDiagram.gif" /></p>
        <h3>Abstraction 
</h3>
        <p>
Abstraction is the process of simplifying an object or system by focusing only on
those parts of the system relevant to the current problem. In OOP, abstraction is
typically implemented using encapsulation. 
</p>
        <h3>Encapsulation 
</h3>
        <p>
Encapsulation hides implementation details of an object from that outside world. Again,
this goes back to our goal of decreasing complexity. We don’t need to understand all
the workings of an object in order to use it. Returning to our Automobile example,
in order to start a car, I need only know to put the key into the ignition and turn
it for a short time. A lot happens inside the car (gasoline is injected, battery is
engaged, sparks fly, gas explodes) but I don’t need to the details of any of that
in order to start the car. In fact, it’s easier for me to focus on starting the car
if I ignore the other things going on within the automobile. 
</p>
        <h3>Decoupling 
</h3>
        <p>
Finally, decoupling is a key point of object oriented programming that simplifies
a system. In a decoupled system, the dependencies between objects are minimized. Encapsulation
helps with this because external objects cannot change the internal attributes of
an object if they cannot access it. 
</p>
        <p>
However, the responsibility for decoupling ultimately rests with the developer. Use
messages to communicate between objects and maintain as little state as possible.
Expose only those attributes needed by others and avoid coding side effects in your
object’s methods. 
</p>
        <p>
In my experience, decoupling is the OOP concept that is ignored the most by people
trying to build object oriented systems. 
</p>
        <h3>Summary 
</h3>
        <p>
As we stated in part 1 of this series, the goal of Object Oriented Programming is
to manage complexity. We do this by modeling our application as many people see their
real world systems – as a set of loosely coupled objects that interact with one another.
This helps us to split a complex problem into a number of more manageable objects.
It also allows us to simplify the management of those objects by encapsulating complexity,
maximizing flexibility through inheritance, and keeping the objects independent of
one another. 
</p>
        <hr />
        <p>
          <font size="1">Thanks to Chris Woodruff, who contributed to this article.</font>
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=6595467f-c388-403b-832a-6b274dd39e2f" />
      </body>
      <title>Intro to OOP, Part 3: OOP Concepts</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,6595467f-c388-403b-832a-6b274dd39e2f.aspx</guid>
      <link>http://www.davidgiard.com/2009/08/06/IntroToOOPPart3OOPConcepts.aspx</link>
      <pubDate>Thu, 06 Aug 2009 11:53:29 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;/p&gt;
&lt;p&gt;
In this section, we will discuss the key concepts important to Object Oriented Programming
(OOP). An object-oriented system has the following characteristics 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Inheritance 
&lt;li&gt;
Polymorphism 
&lt;li&gt;
Abstraction 
&lt;li&gt;
Encapsulation 
&lt;li&gt;
Decoupling 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Some systems (and some languages) don’t fully support all the above constructs and
still refer to themselves as “object-oriented”. This is a matter of some debate, but
it is my opinion that a language or system must implement each of the above concepts
in some way in order to be considered object oriented. 
&lt;/p&gt;
&lt;h3&gt;Inheritance 
&lt;/h3&gt;
&lt;p&gt;
Inheritance is the ability to create a class based on an existing class. In this model,
the existing class is known as the “parent class”, or “base class” and the new class
is known as the “child class” or “derived class”. By default, a child class will inherit
all properties and methods of its parent class. 
&lt;/p&gt;
&lt;p&gt;
In C#, we inherit a class from a parent class by adding appending a colon and the
name of the parent class to the child class definition, as in the following example: 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; ChildClass
: ParentClass {} &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Marking a method as virtual in a parent class allows it to be overridden in a child
class. This means that the method can be replaced in the child class with new code. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;virtual&lt;/span&gt; Int32
DoMath (Int32 num1, Int32 num2) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt; return&lt;/span&gt; num1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; num2;
} &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We can then use the “override” keyword in the child class’s method to replace the
code in the parent’s class method, as shown below 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;override&lt;/span&gt; Int32
DoMath (Int32 num1, Int32 num2) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt; return&lt;/span&gt; num1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;-&lt;/span&gt; num2;
} &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
It is possible to have multiple child classes that inherit from the same parent class.
In some languages (but not in C#), it is also possible for a child class to inherit
from multiple parent classes. 
&lt;/p&gt;
&lt;p&gt;
As a general rule, if I find myself writing a lot of conditional logic in a class’s
methods – depending on how the object is created or the environment, the class runs
one of several blocks of code – I will consider refactoring that class into a number
of subclasses. Each class will inherit from a common parent class but the conditional
logic is eliminated because each subclass contains only the code relevant to how it
is created. 
&lt;/p&gt;
&lt;h3&gt;Interface Inheritance 
&lt;/h3&gt;
&lt;p&gt;
Inheriting from an interface is similar to inheriting from a class, except the parent
class does not contain any implementation, so each subclass contains all method code
it needs to run. This is useful if a set of subclasses need to share the same public
members but do not have any code in common. Interfaces also have the advantage that
a single class can inherit from more than one interface. 
&lt;/p&gt;
&lt;h3&gt;Polymorphism 
&lt;/h3&gt;
&lt;p&gt;
Earlier, we said that objects communicate by passing messages via their public interface. 
&lt;/p&gt;
&lt;p&gt;
Polymorphism is the ability for different objects to respond to the same messages
in a different, but appropriate, way. In OOP, this is done using inheritance. Because
a child class inherits properties, fields and methods from a parent class, we can
have several different child classes sharing the same public members. Each of these
classes would therefore accept the same message types. However, each base class may
override some of the behavior of the base class and may do so in a way appropriate
to itself. 
&lt;/p&gt;
&lt;p&gt;
For example, we may create a Vehicle class that has a method Drive() that accepts
no parameters. 
&lt;/p&gt;
&lt;p&gt;
We can derive two child classes - Car and Airplane - from Vehicle and they will each
inherit the Drive method. But driving a car is not like driving an airplane, so each
of these methods would be overridden and the overridden child methods would be very
different from one another. Calling the Car’s Drive method would cause the axels to
turn and rotate the four wheels beneath the car. Calling the Airplane’s Drive method
would explode jet fuel and propel the airplane through the sky at a high velocity. 
&lt;/p&gt;
&lt;p&gt;
This is an example of two objects (Car and Airplane) that accept the same message
(Drive) and respond differently but appropriately. 
&lt;/p&gt;
&lt;p&gt;
The C# code for this set of classes would look similar to the following 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Vehicle
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt; public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;virtual&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Drive()
{ Console.WriteLine(“Driving…”); } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Automobile:
Vehicle { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt; public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;override&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Drive()
{ Console.WriteLine(“Turn axel and wheels and tires…”); } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Airplane:
Vehicle { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt; public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;override&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Drive()
{ Console.WriteLine(“Explode jet fuel and propel through the air…”); } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The picture below shows the hierarchy of classes that inherit from a single parent
class.&lt;img border=0 src="http://www.davidgiard.com/content/binary/OOPInheritanceClassDiagram.gif"&gt;
&lt;/p&gt;
&lt;h3&gt;Abstraction 
&lt;/h3&gt;
&lt;p&gt;
Abstraction is the process of simplifying an object or system by focusing only on
those parts of the system relevant to the current problem. In OOP, abstraction is
typically implemented using encapsulation. 
&lt;/p&gt;
&lt;h3&gt;Encapsulation 
&lt;/h3&gt;
&lt;p&gt;
Encapsulation hides implementation details of an object from that outside world. Again,
this goes back to our goal of decreasing complexity. We don’t need to understand all
the workings of an object in order to use it. Returning to our Automobile example,
in order to start a car, I need only know to put the key into the ignition and turn
it for a short time. A lot happens inside the car (gasoline is injected, battery is
engaged, sparks fly, gas explodes) but I don’t need to the details of any of that
in order to start the car. In fact, it’s easier for me to focus on starting the car
if I ignore the other things going on within the automobile. 
&lt;/p&gt;
&lt;h3&gt;Decoupling 
&lt;/h3&gt;
&lt;p&gt;
Finally, decoupling is a key point of object oriented programming that simplifies
a system. In a decoupled system, the dependencies between objects are minimized. Encapsulation
helps with this because external objects cannot change the internal attributes of
an object if they cannot access it. 
&lt;/p&gt;
&lt;p&gt;
However, the responsibility for decoupling ultimately rests with the developer. Use
messages to communicate between objects and maintain as little state as possible.
Expose only those attributes needed by others and avoid coding side effects in your
object’s methods. 
&lt;/p&gt;
&lt;p&gt;
In my experience, decoupling is the OOP concept that is ignored the most by people
trying to build object oriented systems. 
&lt;/p&gt;
&lt;h3&gt;Summary 
&lt;/h3&gt;
&lt;p&gt;
As we stated in part 1 of this series, the goal of Object Oriented Programming is
to manage complexity. We do this by modeling our application as many people see their
real world systems – as a set of loosely coupled objects that interact with one another.
This helps us to split a complex problem into a number of more manageable objects.
It also allows us to simplify the management of those objects by encapsulating complexity,
maximizing flexibility through inheritance, and keeping the objects independent of
one another. 
&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;
&lt;font size=1&gt;Thanks to Chris Woodruff, who contributed to this article.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=6595467f-c388-403b-832a-6b274dd39e2f" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,6595467f-c388-403b-832a-6b274dd39e2f.aspx</comments>
      <category>Back To Basics</category>
      <category>OOP</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=d5c64cdf-eaa9-4593-a88f-912c0232cf09</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,d5c64cdf-eaa9-4593-a88f-912c0232cf09.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,d5c64cdf-eaa9-4593-a88f-912c0232cf09.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=d5c64cdf-eaa9-4593-a88f-912c0232cf09</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <h3>Key Object Concepts 
</h3>
        <p>
Objects are essentially a collection of structured data stored in memory. An object
is based on a class that defines how to create an object. 
</p>
        <p>
In this section, I will describe the following concepts. 
</p>
        <ul>
          <li>
Classes 
</li>
          <li>
Class members 
<ul><li>
Properties and Fields 
</li><li>
Methods 
</li><li>
Events 
</li></ul></li>
          <li>
Instances 
</li>
          <li>
Static Types 
</li>
          <li>
Interfaces 
</li>
          <li>
Message Passing 
</li>
        </ul>
        <p>
Because all these terms are interrelated, it is difficult to discuss one without mentioning
the others. So be patient if I mention a term before I define it – I will get to the
definition shortly. 
</p>
        <h3>Classes 
</h3>
        <p>
A class is a definition for an object. It describes the attributes, such as properties,
fields and methods (more on this later) of an object. It may also set default values
and implementations for these attributes. Think of a class as a blueprint we can use
to help us build an object. Generally, we work with classes only at design-time, defining
the attributes appropriate for that class. In most cases, we do not work directly
with classes while a program is running. 
</p>
        <p>
In C#, a class is defined with the following code
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> &lt;CLASSNAME&gt;{} </span>
        </pre>
        <p>
So to create a class named <em>MyClass</em>, we use the declaration:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> MyClass{} </span>
        </pre>
        <h3>Public Members 
</h3>
        <p>
A class (and an object) exposes a finite set of publicly-exposed members. Members
are methods, properties and fields (defined below). This public interface is how you
interact with an object. An object may be capable of far more than what it exposes
by its public interface, but these other capabilities are not seen directly by the
outside world. Keeping object interfaces simple is one way that an object can help
simplify a complex system. 
</p>
        <h3>Properties and Fields 
</h3>
        <p>
Properties and fields describe static data associated with a class. In C#, fields
are implemented with the following syntax:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">&lt;DATETYPE&gt;</span> &lt;FIELDNAME&gt; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> ,VALUE; </span>
        </pre>
        <p>
as in the sample code below:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Color <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> “White”; </span>
        </pre>
        <p>
You can access this property with syntax like the following 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">obj.Color <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> “Black”; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> thisColor <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> obj.Color; </span>
        </pre>
        <p>
A property is similar to a field, but a property provides a “Getter” and “Setter”
– methods that run when you attempt access the value of a property. This allows a
developer to add code that will automatically run whenever a field’s value is retrieved
or assigned. This code might perform validation or calculate a value on the fly. The
C# syntax for fields looks like the following 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> _color; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Color
{ get { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"> return</span> _color:
} set { _color <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} } </span>
        </pre>
        <p>
 Beginning with C# 3.0, this syntax can be shortened to 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Color{get;
set; }</span>
        </pre>
        <p>
The syntax for working with a property is identical to that for working with a field. 
</p>
        <h3>Methods 
</h3>
        <p>
A method is a discrete section of code that is associated with a class and therefore
with an object. Some methods return a value; others just run code and return nothing.
In C#, sample syntax for a method that returns nothing is shown below. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> WriteSomething(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> messageToWrite)
{ Console.WriteLine (messageToWrite); }</span>
        </pre>
        <p>
To return a value, in place of “void” in the method declaration, we specify the data
type returned. Below is an example. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> Int32
AddNumber(Int32 num1, Int32 num2) { Int32 sumOfNumbers <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> num1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> num2; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> sumOfNumbers;
}</span>
        </pre>
        <p>
We can call a method of an object with syntax like the following 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">obj1.WriteSomething(“Hello
World”); Int32 sum <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> obj1.AddNumbers(2,2);</span>
        </pre>
        <h3>Instances 
</h3>
        <p>
So far, we’ve been talking about objects without defining what an object is. An object
is an instance of a class – it represents a specific set of data. 
</p>
        <p>
We said before that a class is like a blueprint. Think of an object as the house,
machine or other device built from that blueprint. Methods, properties and fields
defined in a class become methods, properties and fields in any object based on that
class. 
</p>
        <p>
It is possible to produce more than one house from the same blueprint. Similarly,
it is possible to create multiple objects from the same class. 
</p>
        <p>
To instantiate an object in C#, we use the following syntax
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">&lt;OBJECTTYPE&gt;
&lt;OBJECTNAME&gt; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> &lt;OBJECTTYPE&gt;();</span>
        </pre>
        <p>
In the Methods sample above, assume the methods were defined in a class named <em>MyMathClass</em>.
Before calling the object's methods, we would first instantiate the object like this:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">MyMathClass
obj1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> MyMathClass();</span>
        </pre>
        <h3>Static Types 
</h3>
        <p>
It is possible to instantiate a class without explicitly creating an instance of that
class. You can do this if the class is defined as “static”, as in the following example.
</p>
        <p>
It is possible to instantiate a class without explicitly creating an instance of that
class. You can do this if the class is defined as “static”, as in the following example.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Math
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span> Int32
MultiplyNumbers(Int32 num1, Int32 num2) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> num1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">*</span> num2;
} } </span>
        </pre>
        <p>
Call this method with the following syntax
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">Int32
product <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> Math.MultiplyNumbers(2,
4); </span>
        </pre>
        <p>
Notice that we did not need to explicitly instantiate an object of type Math. 
For static classes, the .Net framework takes care of this for us.
</p>
        <h3>Constructors 
</h3>
        <p>
A constructor is a special type of method that runs when an object is first created.
This is a good place to put initialization code for your object. In C#, a constructor
is written by creating a method with the same name as the class. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> MyClass 
<br />
{ 
<br /><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">   public</span> MyClass() 
<br />
   { 
<br /><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">   //
Initialization code goes here 
<br />
   } 
<br /></span></span>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">}</span>
          </span>
        </pre>
        <p>
You may create constructors that accept parameters, such as in the following C# code 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> MyClass 
<br />
{ 
<br /><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">   public</span> MyClass(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> x, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> y) 
<br />
   { 
<br /><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">      //
Initialization code goes here 
<br />
   } 
<br />
}</span></span>
        </pre>
        <h3>Events 
</h3>
        <p>
An event is a notification by an object that something has happened. This something
might be user input, such as a mouse-click on a form or it can be something less tangible,
such as a customer exceeding his credit limit. Other objects may or may not respond
to these events. Generally the object raising an event does not know how it will be
consumed. 
</p>
        <h3>Interfaces 
</h3>
        <p>
An interface looks like a class in that it can have properties, fields and methods.
The difference is that the properties and methods contain no implementation code.
Interfaces are used only to define the public members of a class. A class implementing
an interface inherits all the public members of that interface and it is up to the
class to provide implementation. 
</p>
        <p>
Below is sample syntax for creating an interface 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">interace
IPerson { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{get; set;} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> LastName
{get; set;} Order GetAllOrders(); }</span>
        </pre>
        <p>
Use code like the following to create a class that implements an interface 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Customer:
IPerson { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> FirstName
{get; set;} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> LastName
{get; set;} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> Order
GetAllOrders() { Order ord <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Order(); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
Code omitted for brevity </span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> ord;
} } </span>
        </pre>
        <h3>Message Passing 
</h3>
        <p>
Objects communicate by passing messages. These messages can be primitive data types,
such as strings and integers; they can be XML; or they can be other objects. 
Generally speaking objects expose public members to accept these messages. This helps
to simplify the communication between objects.
</p>
        <p>
In this section, we learned the basics of objects, their definitions, their members
and how to work with them. In the next section, we’ll introduce Object Oriented Programming
constructs and how these concepts are implemented using objects.
</p>
        <hr />
        <p>
          <font size="1">Thanks to Chris Woodruff, who contributed to this article.</font>
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=d5c64cdf-eaa9-4593-a88f-912c0232cf09" />
      </body>
      <title>Intro To OOP, Part 2: Understanding Objects</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,d5c64cdf-eaa9-4593-a88f-912c0232cf09.aspx</guid>
      <link>http://www.davidgiard.com/2009/08/05/IntroToOOPPart2UnderstandingObjects.aspx</link>
      <pubDate>Wed, 05 Aug 2009 04:36:25 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;/p&gt;
&lt;h3&gt;Key Object Concepts 
&lt;/h3&gt;
&lt;p&gt;
Objects are essentially a collection of structured data stored in memory. An object
is based on a class that defines how to create an object. 
&lt;/p&gt;
&lt;p&gt;
In this section, I will describe the following concepts. 
&lt;ul&gt;
&lt;li&gt;
Classes 
&lt;li&gt;
Class members 
&lt;ul&gt;
&lt;li&gt;
Properties and Fields 
&lt;li&gt;
Methods 
&lt;li&gt;
Events 
&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
Instances 
&lt;li&gt;
Static Types 
&lt;li&gt;
Interfaces 
&lt;li&gt;
Message Passing 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Because all these terms are interrelated, it is difficult to discuss one without mentioning
the others. So be patient if I mention a term before I define it – I will get to the
definition shortly. 
&lt;/p&gt;
&lt;h3&gt;Classes 
&lt;/h3&gt;
&lt;p&gt;
A class is a definition for an object. It describes the attributes, such as properties,
fields and methods (more on this later) of an object. It may also set default values
and implementations for these attributes. Think of a class as a blueprint we can use
to help us build an object. Generally, we work with classes only at design-time, defining
the attributes appropriate for that class. In most cases, we do not work directly
with classes while a program is running. 
&lt;/p&gt;
&lt;p&gt;
In C#, a class is defined with the following code
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; &amp;lt;CLASSNAME&amp;gt;{} &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
So to create a class named &lt;em&gt;MyClass&lt;/em&gt;, we use the declaration:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; MyClass{} &lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Public Members 
&lt;/h3&gt;
&lt;p&gt;
A class (and an object) exposes a finite set of publicly-exposed members. Members
are methods, properties and fields (defined below). This public interface is how you
interact with an object. An object may be capable of far more than what it exposes
by its public interface, but these other capabilities are not seen directly by the
outside world. Keeping object interfaces simple is one way that an object can help
simplify a complex system. 
&lt;/p&gt;
&lt;h3&gt;Properties and Fields 
&lt;/h3&gt;
&lt;p&gt;
Properties and fields describe static data associated with a class. In C#, fields
are implemented with the following syntax:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;&amp;lt;DATETYPE&amp;gt;&lt;/span&gt; &amp;lt;FIELDNAME&amp;gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; ,VALUE; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
as in the&amp;nbsp;sample code below:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Color &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; “White”; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
You can access this property with syntax like the following 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;obj.Color &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; “Black”; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; thisColor &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; obj.Color; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
A property is similar to a field, but a property provides a “Getter” and “Setter”
– methods that run when you attempt access the value of a property. This allows a
developer to add code that will automatically run whenever a field’s value is retrieved
or assigned. This code might perform validation or calculate a value on the fly. The
C# syntax for fields looks like the following 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; _color; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Color
{ get { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt; return&lt;/span&gt; _color:
} set { _color &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;nbsp;Beginning with C# 3.0, this syntax can be shortened to 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Color{get;
set; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The syntax for working with a property is identical to that for working with a field. 
&lt;/p&gt;
&lt;h3&gt;Methods 
&lt;/h3&gt;
&lt;p&gt;
A method is a discrete section of code that is associated with a class and therefore
with an object. Some methods return a value; others just run code and return nothing.
In C#, sample syntax for a method that returns nothing is shown below. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; WriteSomething(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; messageToWrite)
{ Console.WriteLine (messageToWrite); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
To return a value, in place of “void” in the method declaration, we specify the data
type returned. Below is an example.&amp;nbsp;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; Int32
AddNumber(Int32 num1, Int32 num2) { Int32 sumOfNumbers &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; num1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; num2; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; sumOfNumbers;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We can call a method of an object with syntax like the following 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;obj1.WriteSomething(“Hello
World”); Int32 sum &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; obj1.AddNumbers(2,2);&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Instances 
&lt;/h3&gt;
&lt;p&gt;
So far, we’ve been talking about objects without defining what an object is. An object
is an instance of a class – it represents a specific set of data. 
&lt;/p&gt;
&lt;p&gt;
We said before that a class is like a blueprint. Think of an object as the house,
machine or other device built from that blueprint. Methods, properties and fields
defined in a class become methods, properties and fields in any object based on that
class. 
&lt;/p&gt;
&lt;p&gt;
It is possible to produce more than one house from the same blueprint. Similarly,
it is possible to create multiple objects from the same class. 
&lt;/p&gt;
&lt;p&gt;
To instantiate an object in C#, we use the following syntax
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&amp;lt;OBJECTTYPE&amp;gt;
&amp;lt;OBJECTNAME&amp;gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; &amp;lt;OBJECTTYPE&amp;gt;();&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In the Methods sample above, assume the methods were defined in a class named &lt;em&gt;MyMathClass&lt;/em&gt;.
Before calling the object's methods, we would first instantiate the object like this:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;MyMathClass
obj1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; MyMathClass();&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Static Types 
&lt;/h3&gt;
&lt;p&gt;
It is possible to instantiate a class without explicitly creating an instance of that
class. You can do this if the class is defined as “static”, as in the following example.
&lt;/p&gt;
&lt;p&gt;
It is possible to instantiate a class without explicitly creating an instance of that
class. You can do this if the class is defined as “static”, as in the following example.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Math
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; Int32
MultiplyNumbers(Int32 num1, Int32 num2) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; num1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;*&lt;/span&gt; num2;
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Call this method with the following syntax
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;Int32
product &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; Math.MultiplyNumbers(2,
4); &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Notice that we did not need to explicitly instantiate an object of type Math.&amp;nbsp;
For static classes, the .Net framework takes care of this for us.
&lt;/p&gt;
&lt;h3&gt;Constructors 
&lt;/h3&gt;
&lt;p&gt;
A constructor is a special type of method that runs when an object is first created.
This is a good place to put initialization code for your object. In C#, a constructor
is written by creating a method with the same name as the class. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; MyClass 
&lt;br&gt;
{ 
&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/span&gt; MyClass() 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;{ 
&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//
Initialization code goes here 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;} 
&lt;br&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
You may create constructors that accept parameters, such as in the following C# code 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; MyClass 
&lt;br&gt;
{ 
&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/span&gt; MyClass(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; x, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; y) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;{ 
&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//
Initialization code goes here 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;} 
&lt;br&gt;
}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Events 
&lt;/h3&gt;
&lt;p&gt;
An event is a notification by an object that something has happened. This something
might be user input, such as a mouse-click on a form or it can be something less tangible,
such as a customer exceeding his credit limit. Other objects may or may not respond
to these events. Generally the object raising an event does not know how it will be
consumed. 
&lt;/p&gt;
&lt;h3&gt;Interfaces 
&lt;/h3&gt;
&lt;p&gt;
An interface looks like a class in that it can have properties, fields and methods.
The difference is that the properties and methods contain no implementation code.
Interfaces are used only to define the public members of a class. A class implementing
an interface inherits all the public members of that interface and it is up to the
class to provide implementation. 
&lt;/p&gt;
&lt;p&gt;
Below is sample syntax for creating an interface 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;interace
IPerson { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{get; set;} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; LastName
{get; set;} Order GetAllOrders(); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Use code like the following to create a class that implements an interface 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Customer:
IPerson { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; FirstName
{get; set;} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; LastName
{get; set;} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; Order
GetAllOrders() { Order ord &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Order(); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
Code omitted for brevity &lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; ord;
} } &lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Message Passing 
&lt;/h3&gt;
&lt;p&gt;
Objects communicate by passing messages. These messages can be primitive data types,
such as strings and integers; they can be XML; or they can be other objects.&amp;nbsp;
Generally speaking objects expose public members to accept these messages. This helps
to simplify the communication between objects.
&lt;/p&gt;
&lt;p&gt;
In this section, we learned the basics of objects, their definitions, their members
and how to work with them. In the next section, we’ll introduce Object Oriented Programming
constructs and how these concepts are implemented using objects.
&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;
&lt;font size=1&gt;Thanks to Chris Woodruff, who contributed to this article.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=d5c64cdf-eaa9-4593-a88f-912c0232cf09" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,d5c64cdf-eaa9-4593-a88f-912c0232cf09.aspx</comments>
      <category>Back To Basics</category>
      <category>OOP</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=8253711b-65eb-46e5-8358-1ea0dced73cc</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,8253711b-65eb-46e5-8358-1ea0dced73cc.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,8253711b-65eb-46e5-8358-1ea0dced73cc.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=8253711b-65eb-46e5-8358-1ea0dced73cc</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        </p>
        <h3>What is OOP? 
</h3>
        <p>
Most people think of their business systems in terms of the interactions of people,
institutions and systems. When describing a system, they often speak in terms of these
interactions between entities. For example, they may describe how a customer communicates
order information to a salesperson that inputs this information into an Order Processing
program. Describing entities and defining how those entities interact with one another
maps very well to Object Oriented Programming (OOP). OOP is a programming paradigm
that focuses on the objects in a software system and the messages passed between those
objects. 
</p>
        <p>
Object Oriented Programming is not a new concept. The ideas behind it were first published
in the 1960s and the SmallTalk language, developed in the 1970s, formalized many of
the programming constructs used in this paradigm. However, OOP gained popularity in
the 1990’s with the increased use of the C++ and the last ten years have seen an explosion
in object-oriented languages C#, Visual Basic.Net and Java. 
</p>
        <p>
In earlier programming models, a program was viewed as a set of methods or subroutines.
A main method would call one or more subroutines that might each in turn call other
subroutines. Each subroutine would complete its task and return control to the method
that called it. Using this approach, developers viewed their programs as a set of
tasks. These tasks became central to their application design. 
</p>
        <h3>Managing Complex Systems 
</h3>
        <p>
It was difficult to manage complexity in this task-oriented approach to programming. 
</p>
        <p>
For example, one couldn’t be certain that a subroutine would not substantially change
variables, files and other parts of the environment. The only way to know for sure
was to examine that subroutine in detail to learn how it worked. In fact, you would
need to also examine any subroutines called by that subroutine, to verify that they
didn’t cause any unexpected side effects. 
</p>
        <p>
Another complexity arises because there tends to be a tight coupling between the subroutines.
Changes to a subroutine often require changes to any routine that called it, making
maintenance difficult. 
</p>
        <p>
Often in such systems, you find code duplicated in multiple places. Making a fundamental
change to how a rule is implemented requires changing code several times – another
maintenance difficulty that adds to complexity. 
</p>
        <p>
The Development process itself adds to the complexity of writing software. On most
projects, we need to consider how to divide development tasks among team members;
merge this code together; manage code deployment; test code modules independently
and together; deploy updates to our application; and fix errors as they are found.
The entire process can be quite complex, particularly if all our modules are tightly
bound together. 
</p>
        <p>
Finally, we cannot ignore the fact that many of the problems we are trying to solve
are complex. This complexity is often our motivation to automate these problems in
the first place. Anything we can do to mitigate that complexity – such as splitting
a problem into manageable components – will assist us in building a solution. 
</p>
        <p>
OOP attempts to address these challenges by focusing on objects instead of tasks.
The whole goal of OOP is to manage complexity. This is done by splitting a complex
system into manageable, independent pieces and only exposing enough of each piece
to allow other objects to work with it. Much of the complexity of the object can be
hidden from the outside world, making the system as a whole easier to understand. 
</p>
        <p>
The following basic principles help to accomplish OOP’s goal of managing complexity. 
</p>
        <ul>
          <li>
Inheritance 
</li>
          <li>
Abstraction 
</li>
          <li>
Encapsulation 
</li>
          <li>
Polymorphism 
</li>
          <li>
Decoupling 
</li>
        </ul>
        <p>
The above concepts are interrelated in that some are used to accomplish others. In
a later article, we will dive into more detail about each. 
</p>
        <p>
Before we can do that, it’s important to understand the basics of objects before you
can grasp Object Oriented Programming.
</p>
        <p>
Next: Intro to OOP, Part 2: Understanding Objects 
</p>
        <hr />
        <p>
          <font size="1">Thanks to Chris Woodruff, who contributed to this article.</font>
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=8253711b-65eb-46e5-8358-1ea0dced73cc" />
      </body>
      <title>Intro To OOP, Part 1: What is OOP?</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,8253711b-65eb-46e5-8358-1ea0dced73cc.aspx</guid>
      <link>http://www.davidgiard.com/2009/08/04/IntroToOOPPart1WhatIsOOP.aspx</link>
      <pubDate>Tue, 04 Aug 2009 13:23:52 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;/p&gt;
&lt;h3&gt;What is OOP? 
&lt;/h3&gt;
&lt;p&gt;
Most people think of their business systems in terms of the interactions of people,
institutions and systems. When describing a system, they often speak in terms of these
interactions between entities. For example, they may describe how a customer communicates
order information to a salesperson that inputs this information into an Order Processing
program. Describing entities and defining how those entities interact with one another
maps very well to Object Oriented Programming (OOP). OOP is a programming paradigm
that focuses on the objects in a software system and the messages passed between those
objects. 
&lt;/p&gt;
&lt;p&gt;
Object Oriented Programming is not a new concept. The ideas behind it were first published
in the 1960s and the SmallTalk language, developed in the 1970s, formalized many of
the programming constructs used in this paradigm. However, OOP gained popularity in
the 1990’s with the increased use of the C++ and the last ten years have seen an explosion
in object-oriented languages C#, Visual Basic.Net and Java. 
&lt;/p&gt;
&lt;p&gt;
In earlier programming models, a program was viewed as a set of methods or subroutines.
A main method would call one or more subroutines that might each in turn call other
subroutines. Each subroutine would complete its task and return control to the method
that called it. Using this approach, developers viewed their programs as a set of
tasks. These tasks became central to their application design. 
&lt;h3&gt;Managing Complex Systems 
&lt;/h3&gt;
&lt;p&gt;
It was difficult to manage complexity in this task-oriented approach to programming. 
&lt;/p&gt;
&lt;p&gt;
For example, one couldn’t be certain that a subroutine would not substantially change
variables, files and other parts of the environment. The only way to know for sure
was to examine that subroutine in detail to learn how it worked. In fact, you would
need to also examine any subroutines called by that subroutine, to verify that they
didn’t cause any unexpected side effects. 
&lt;/p&gt;
&lt;p&gt;
Another complexity arises because there tends to be a tight coupling between the subroutines.
Changes to a subroutine often require changes to any routine that called it, making
maintenance difficult. 
&lt;/p&gt;
&lt;p&gt;
Often in such systems, you find code duplicated in multiple places. Making a fundamental
change to how a rule is implemented requires changing code several times – another
maintenance difficulty that adds to complexity. 
&lt;/p&gt;
&lt;p&gt;
The Development process itself adds to the complexity of writing software. On most
projects, we need to consider how to divide development tasks among team members;
merge this code together; manage code deployment; test code modules independently
and together; deploy updates to our application; and fix errors as they are found.
The entire process can be quite complex, particularly if all our modules are tightly
bound together. 
&lt;/p&gt;
&lt;p&gt;
Finally, we cannot ignore the fact that many of the problems we are trying to solve
are complex. This complexity is often our motivation to automate these problems in
the first place. Anything we can do to mitigate that complexity – such as splitting
a problem into manageable components – will assist us in building a solution. 
&lt;/p&gt;
&lt;p&gt;
OOP attempts to address these challenges by focusing on objects instead of tasks.
The whole goal of OOP is to manage complexity. This is done by splitting a complex
system into manageable, independent pieces and only exposing enough of each piece
to allow other objects to work with it. Much of the complexity of the object can be
hidden from the outside world, making the system as a whole easier to understand. 
&lt;/p&gt;
&lt;p&gt;
The following basic principles help to accomplish OOP’s goal of managing complexity. 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Inheritance 
&lt;li&gt;
Abstraction 
&lt;li&gt;
Encapsulation 
&lt;li&gt;
Polymorphism 
&lt;li&gt;
Decoupling 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The above concepts are interrelated in that some are used to accomplish others. In
a later article,&amp;nbsp;we will dive into more detail about each. 
&lt;/p&gt;
&lt;p&gt;
Before we can do that, it’s important to understand the basics of objects before you
can grasp Object Oriented Programming.
&lt;/p&gt;
&lt;p&gt;
Next: Intro&amp;nbsp;to OOP, Part 2: Understanding Objects&amp;nbsp;
&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;
&lt;font size=1&gt;Thanks to Chris Woodruff, who contributed to this article.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=8253711b-65eb-46e5-8358-1ea0dced73cc" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,8253711b-65eb-46e5-8358-1ea0dced73cc.aspx</comments>
      <category>Back To Basics</category>
      <category>OOP</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=281001ac-1ed6-4232-8d0d-ae3aabe0095f</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,281001ac-1ed6-4232-8d0d-ae3aabe0095f.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,281001ac-1ed6-4232-8d0d-ae3aabe0095f.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=281001ac-1ed6-4232-8d0d-ae3aabe0095f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <img border="0" src="http://www.davidgiard.com/content/binary/TechnologyAndFriends.gif" />
        </p>
        <p>
          <strong>Episode 18</strong>
        </p>
        <p>
Halfway through the first West Michigan .Net University, Chris Woodruff and Bill Miller
sat down with me to discuss why they organized it and how it was going so far.
</p>
        <p>
For more information on this event, you can visit <a href="http://dodn.org/WestMichiganDotNetU/" target="_blank">dodn.org/WestMichiganDotNetU</a> or
read my earlier posts <a href="http://www.davidgiard.com/2009/04/06/WestMichiganNetUniversityRecap.aspx">here</a> and <a href="http://www.davidgiard.com/2009/03/28/WestMichiganNetUniversityIsComing.aspx">here</a><br /></p>
        <p>
          <object id="viddler_9ecbbfbf" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="437" height="370">
            <param name="_cx" value="11562" />
            <param name="_cy" value="9789" />
            <param name="FlashVars" value="" />
            <param name="Movie" value="http://www.viddler.com/player/9ecbbfbf/" />
            <param name="Src" value="http://www.viddler.com/player/9ecbbfbf/" />
            <param name="WMode" value="Window" />
            <param name="Play" value="-1" />
            <param name="Loop" value="-1" />
            <param name="Quality" value="High" />
            <param name="SAlign" value="" />
            <param name="Menu" value="-1" />
            <param name="Base" value="" />
            <param name="AllowScriptAccess" value="always" />
            <param name="Scale" value="ShowAll" />
            <param name="DeviceFont" value="0" />
            <param name="EmbedMovie" value="0" />
            <param name="BGColor" value="" />
            <param name="SWRemote" value="" />
            <param name="MovieData" value="" />
            <param name="SeamlessTabbing" value="1" />
            <param name="Profile" value="0" />
            <param name="ProfileAddress" value="" />
            <param name="ProfilePort" value="0" />
            <param name="AllowNetworking" value="all" />
            <param name="AllowFullScreen" value="true" />
            <embed src="http://www.viddler.com/player/9ecbbfbf/" width="437" height="370" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" name="viddler_9ecbbfbf">
            </embed>
          </object>
        </p>
        <p>
17 mins, 50 secs
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=281001ac-1ed6-4232-8d0d-ae3aabe0095f" />
      </body>
      <title>Chris Woodruff and Bill Miller on the West Michigan .Net University</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,281001ac-1ed6-4232-8d0d-ae3aabe0095f.aspx</guid>
      <link>http://www.davidgiard.com/2009/04/27/ChrisWoodruffAndBillMillerOnTheWestMichiganNetUniversity.aspx</link>
      <pubDate>Mon, 27 Apr 2009 13:46:43 GMT</pubDate>
      <description>&lt;p&gt;
&lt;img border=0 src="http://www.davidgiard.com/content/binary/TechnologyAndFriends.gif"&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Episode 18&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Halfway through the first West Michigan .Net University, Chris Woodruff and Bill Miller
sat down with me to discuss why they organized it and how it was going so far.
&lt;/p&gt;
&lt;p&gt;
For more information on this event, you can visit &lt;a href="http://dodn.org/WestMichiganDotNetU/" target=_blank&gt;dodn.org/WestMichiganDotNetU&lt;/a&gt; or
read my earlier posts &lt;a href="http://www.davidgiard.com/2009/04/06/WestMichiganNetUniversityRecap.aspx"&gt;here&lt;/a&gt; and &lt;a href="http://www.davidgiard.com/2009/03/28/WestMichiganNetUniversityIsComing.aspx"&gt;here&lt;/a&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;object id=viddler_9ecbbfbf classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=437 height=370&gt;
&lt;param name="_cx" value="11562"&gt;
&lt;param name="_cy" value="9789"&gt;
&lt;param name="FlashVars" value=""&gt;
&lt;param name="Movie" value="http://www.viddler.com/player/9ecbbfbf/"&gt;
&lt;param name="Src" value="http://www.viddler.com/player/9ecbbfbf/"&gt;
&lt;param name="WMode" value="Window"&gt;
&lt;param name="Play" value="-1"&gt;
&lt;param name="Loop" value="-1"&gt;
&lt;param name="Quality" value="High"&gt;
&lt;param name="SAlign" value=""&gt;
&lt;param name="Menu" value="-1"&gt;
&lt;param name="Base" value=""&gt;
&lt;param name="AllowScriptAccess" value="always"&gt;
&lt;param name="Scale" value="ShowAll"&gt;
&lt;param name="DeviceFont" value="0"&gt;
&lt;param name="EmbedMovie" value="0"&gt;
&lt;param name="BGColor" value=""&gt;
&lt;param name="SWRemote" value=""&gt;
&lt;param name="MovieData" value=""&gt;
&lt;param name="SeamlessTabbing" value="1"&gt;
&lt;param name="Profile" value="0"&gt;
&lt;param name="ProfileAddress" value=""&gt;
&lt;param name="ProfilePort" value="0"&gt;
&lt;param name="AllowNetworking" value="all"&gt;
&lt;param name="AllowFullScreen" value="true"&gt;
&lt;embed src="http://www.viddler.com/player/9ecbbfbf/" width="437" height="370" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" name="viddler_9ecbbfbf"&gt;&lt;/embed&gt;
&lt;/object&gt;
&lt;/p&gt;
&lt;p&gt;
17 mins, 50 secs
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=281001ac-1ed6-4232-8d0d-ae3aabe0095f" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,281001ac-1ed6-4232-8d0d-ae3aabe0095f.aspx</comments>
      <category>Back To Basics</category>
      <category>Interviews</category>
      <category>Public Speaking</category>
      <category>Technology and Friends</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=fa6fcf21-3177-43b6-becb-5e1c9e232bfe</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,fa6fcf21-3177-43b6-becb-5e1c9e232bfe.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,fa6fcf21-3177-43b6-becb-5e1c9e232bfe.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=fa6fcf21-3177-43b6-becb-5e1c9e232bfe</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last year, I noticed there were more local community-driven events than I had time
to attend.  I love seeing that kind of enthusiasm for technology among the community.  
</p>
        <p>
These events are great for me because - although I've been in software development
for many years - I love to learn and frequently jump at the chance to learn from and
share ideas with other experienced developers and architects.
</p>
        <p>
I have noticed that most of these events targeted experienced developers.  Topics
tended to be moderate to advanced and speakers assumed a certain level of expertise
from their audience in order to grasp their talk.  
</p>
        <p>
This can be frustrating for less-experienced developers.  It's difficult to understand
the details of NHibernate when you are still trying to figure out how to write simple
queries against SQL Server.
</p>
        <p>
Chris Woodruff first described to me the idea of a .Net University - a community event
targeted at those new to .Net programming.  All the content would be introductory,
allowing people to learn fundamentals.
</p>
        <p>
I liked the idea immediately.  When I was a trainer, I always spent time on the
first day reviewing the basic prerequisites of the class material.  I knew that
no one would understand anything about web development if they didn't know how to
construct an HTML document.  
</p>
        <p>
So Chris is now organizing the West Michigan .Net University that will be held April
4 in Grand Rapids.  
</p>
        <p>
The morning sessions will all be introductory and the afternoon will feature more
intermediate topics.  Each session will be about 3 hours - longer than at a typical
code camp.
</p>
        <p>
I'll be there delivering a morning session titled SQL 101, in which I'll explain the
basics of working with a relational database - SQL Server in particular.  I"ll
describe how to work with data and how to use code and tools to interact with SQL
Server.  I've been preparing for this talk by writing a <a href="http://www.davidgiard.com/CategoryView,category,Back%2BTo%2BBasics.aspx">'Back
To Basics' series </a>on this blog.
</p>
        <p>
Other sessions include .Net Bootcamp; Intro To ASP.Net and AJAX; and Real World Architecture. 
You can view the complete session list at <a href="http://dodn.org/WestMichiganDotNetU/Sessions.aspx" target="_blank">http://dodn.org/WestMichiganDotNetU/Sessions.aspx</a></p>
        <p>
A lot of great speakers have signed up for this event so I'm counting on some great
sessions.  
<br /></p>
        <p>
          <a href="http://www.dayofdotnet.org/WestMichiganDotNetU/" target="_blank">
            <img alt="WM .Net University April 4, 2009 - I'll be there!" src="http://www.dayofdotnet.org/WestMichiganDotNetU/images/Site-Badge-I.gif" />
          </a>
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=fa6fcf21-3177-43b6-becb-5e1c9e232bfe" />
      </body>
      <title>West Michigan .Net University is coming</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,fa6fcf21-3177-43b6-becb-5e1c9e232bfe.aspx</guid>
      <link>http://www.davidgiard.com/2009/03/28/WestMichiganNetUniversityIsComing.aspx</link>
      <pubDate>Sat, 28 Mar 2009 22:35:47 GMT</pubDate>
      <description>&lt;p&gt;
Last year, I noticed there were more local community-driven events than I had time
to attend.&amp;nbsp; I love seeing that kind of enthusiasm for technology among the community.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
These events are great for me because - although I've been in software development
for many years - I love to learn and frequently jump at the chance to learn from and
share ideas with other experienced developers and architects.
&lt;/p&gt;
&lt;p&gt;
I have noticed that most of these events targeted experienced developers.&amp;nbsp; Topics
tended to be moderate to advanced and speakers assumed a certain level of expertise
from their audience in order to grasp their talk.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
This can be frustrating for less-experienced developers.&amp;nbsp; It's difficult to understand
the details of NHibernate when you are still trying to figure out how to write simple
queries against SQL Server.
&lt;/p&gt;
&lt;p&gt;
Chris Woodruff first described to me the idea of a .Net University - a community event
targeted at those new to .Net programming.&amp;nbsp; All the content would be introductory,
allowing people to learn fundamentals.
&lt;/p&gt;
&lt;p&gt;
I liked the idea immediately.&amp;nbsp; When I was a trainer, I always spent time on the
first day reviewing the basic prerequisites of the class material.&amp;nbsp; I knew that
no one would understand anything about web development if they didn't know how to
construct an HTML document.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
So Chris is now organizing the West Michigan .Net University that will be held April
4 in Grand Rapids.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
The morning sessions will all be introductory and the afternoon will feature more
intermediate topics.&amp;nbsp; Each session will be about 3 hours - longer than at a typical
code camp.
&lt;/p&gt;
&lt;p&gt;
I'll be there delivering a morning session titled SQL 101, in which I'll explain the
basics of working with a relational database - SQL Server in particular.&amp;nbsp; I"ll
describe how to work with data and how to use code and tools to interact with SQL
Server.&amp;nbsp; I've been preparing for this talk by writing a &lt;a href="http://www.davidgiard.com/CategoryView,category,Back%2BTo%2BBasics.aspx"&gt;'Back
To Basics' series &lt;/a&gt;on this blog.
&lt;/p&gt;
&lt;p&gt;
Other sessions include .Net Bootcamp; Intro To ASP.Net and AJAX; and Real World Architecture.&amp;nbsp;
You can view the complete session list at &lt;a href="http://dodn.org/WestMichiganDotNetU/Sessions.aspx" target=_blank&gt;http://dodn.org/WestMichiganDotNetU/Sessions.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
A lot of great speakers have signed up for this event so I'm counting on some great
sessions.&amp;nbsp; 
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dayofdotnet.org/WestMichiganDotNetU/" target=_blank&gt;&lt;img alt="WM .Net University April 4, 2009 - I'll be there!" src="http://www.dayofdotnet.org/WestMichiganDotNetU/images/Site-Badge-I.gif"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=fa6fcf21-3177-43b6-becb-5e1c9e232bfe" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,fa6fcf21-3177-43b6-becb-5e1c9e232bfe.aspx</comments>
      <category>Back To Basics</category>
      <category>Community</category>
      <category>Public Speaking</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=6257f4a8-134a-4f18-9372-7c38ed4c7219</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,6257f4a8-134a-4f18-9372-7c38ed4c7219.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,6257f4a8-134a-4f18-9372-7c38ed4c7219.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=6257f4a8-134a-4f18-9372-7c38ed4c7219</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        <div style="BORDER-RIGHT: thin solid; BORDER-TOP: thin solid; MARGIN: 5px; BORDER-LEFT: thin solid; WIDTH: 80%; BORDER-BOTTOM: thin solid; BACKGROUND-COLOR: silver">
          <p>
NOTE: 
</p>
          <p>
For demos in this article, we will use a table named Customer that contains the following
columns: 
</p>
          <table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border="1">
            <tbody>
              <tr>
                <td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left">
                  <font size="1">Name </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left">
                  <font size="1">Data Type </font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">FirstName</font>
                </td>
                <td>
                  <font size="1">nvarchar(50)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">LastName</font>
                </td>
                <td>
                  <font size="1">nvarchar(50)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">StreetAddress</font>
                </td>
                <td>
                  <font size="1">nvarchar(255)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">City</font>
                </td>
                <td>
                  <font size="1">nvarchar(255)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">State</font>
                </td>
                <td>
                  <font size="1">char(2)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">ZipCode</font>
                </td>
                <td>
                  <font size="1">nvarchar(10)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">TotalSales</font>
                </td>
                <td>
                  <font size="1">decimal(18,2)</font>
                </td>
              </tr>
            </tbody>
          </table>
          <p>
In addition, I created a primary key on the CustID column and set it to autoincrement
by setting the following properties: 
</p>
          <table style="MARGIN-LEFT: 40px" border="0">
            <tbody>
              <tr>
                <td>
Is Identity 
</td>
                <td>
Yes 
</td>
              </tr>
              <tr>
                <td>
Identity Seed 
</td>
                <td>
1 
</td>
              </tr>
              <tr>
                <td>
Identity Seed 
</td>
                <td>
1 
</td>
              </tr>
            </tbody>
          </table>
          <p>
Afer adding a couple rows to the table, the data looks like this. 
</p>
          <table border="1">
            <tbody>
              <tr>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">CustID </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">FirstName </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">LastName </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">StreetAddress </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">City </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">State </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">ZipCode </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">TotalSales </font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">1</font>
                </td>
                <td>
                  <font size="1">Steve</font>
                </td>
                <td>
                  <font size="1">Smith</font>
                </td>
                <td>
                  <font size="1">900 Belle St</font>
                </td>
                <td>
                  <font size="1">Detroit</font>
                </td>
                <td>
                  <font size="1">MI</font>
                </td>
                <td>
                  <font size="1">48888</font>
                </td>
                <td>
                  <font size="1">5000.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">2</font>
                </td>
                <td>
                  <font size="1">Ryan</font>
                </td>
                <td>
                  <font size="1">Miller</font>
                </td>
                <td>
                  <font size="1">1 Shutout Ct</font>
                </td>
                <td>
                  <font size="1">Buffalo</font>
                </td>
                <td>
                  <font size="1">NY</font>
                </td>
                <td>
                  <font size="1">32323</font>
                </td>
                <td>
                  <font size="1">250.00</font>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <p>
We can use the INSERT command to add a new row to this table. The syntax of the INSERT
command is 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INSERT</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INTO</span> [TableName]
    (     [Column List]     ) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">VALUES</span>     (
    [Values List]     ) </span>
        </pre>
        <p>
We can insert a row for a new Customer - Brad Van Pelt - with the following code. 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INSERT</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INTO</span> Customer
    (     FirstName,     LastName,
    StreetAddress,     City,     State,
    ZipCode,     TotalSales     ) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">VALUES</span>     (
    <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'Brad'</span>,
    <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'Van
Pelt'</span>,     <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'99
Linebaker Ln'</span>,     <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'Owosso'</span>,
    <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'MI'</span>,
    <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'47777'</span>,
    4000     ) </span>
        </pre>
        <p>
Notice that each column name in the first set of parentheses matches a value in the
second set of parentheses: 'Brad' with <i>FirstName</i>, 'Van Pelt' with <i>LastName</i> and
so on. Notice also that we did not provide a value for the <i>CustID</i> column. This
is because <i>CustID</i> is an identity column and, therefore, gets populated with
an incremented number when a new row is added. 
</p>
        <p>
After executing the above INSERT statement, our data should look like this: 
</p>
        <table border="1">
          <tbody>
            <tr>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">CustID </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">FirstName </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">LastName </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">StreetAddress </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">City </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">State </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">ZipCode </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">TotalSales </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">1</font>
              </td>
              <td>
                <font size="1">Steve</font>
              </td>
              <td>
                <font size="1">Smith</font>
              </td>
              <td>
                <font size="1">900 Belle St</font>
              </td>
              <td>
                <font size="1">Detroit</font>
              </td>
              <td>
                <font size="1">MI</font>
              </td>
              <td>
                <font size="1">48888</font>
              </td>
              <td>
                <font size="1">5000.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">2</font>
              </td>
              <td>
                <font size="1">Ryan</font>
              </td>
              <td>
                <font size="1">Miller</font>
              </td>
              <td>
                <font size="1">1 Shutout Ct</font>
              </td>
              <td>
                <font size="1">Buffalo</font>
              </td>
              <td>
                <font size="1">NY</font>
              </td>
              <td>
                <font size="1">32323</font>
              </td>
              <td>
                <font size="1">250.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">3</font>
              </td>
              <td>
                <font size="1">Brad</font>
              </td>
              <td>
                <font size="1">Van Pelt</font>
              </td>
              <td>
                <font size="1">99 Linebaker Ln</font>
              </td>
              <td>
                <font size="1">Owosso</font>
              </td>
              <td>
                <font size="1">MI</font>
              </td>
              <td>
                <font size="1">47777</font>
              </td>
              <td>
                <font size="1">4000.00</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
In this article, we showed how to use T-SQL's INSERT, UPDATE, and DELETE commands
to modify the data in a table. 
</p>
        <p>
The new customer was automatically assigned a <i>CustID</i> value of 3. Because this
value uniquely identifies the newly-added row, we can use it to find and update that
row. The syntax to update a row in SQL Server is 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">UPDATE</span> [Table]
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> [Column1]
= [New Value 1],         [Column2] = [New
Value 2],         [Column3] = [New Value 3],
        etc...     <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> [Filter
Condition] </span>
        </pre>
        <p>
Only rows that match the filter condition will be updated and only those columns specified
in the SET clause will be updated.  We will use the following command to update
the <i>StreetAddress</i>, <i>City</i>, <i>State</i> and <i>ZipCdoe</i> columns of
Customer 3:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">UPDATE</span> Customer
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> StreetAddress
= <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'100
Safety St'</span>,         City=<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'New
York'</span>,         State=<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'NY'</span>         ZipCode=<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'01111'</span>     <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> CustID
= 3 </span>
        </pre>
        <p>
After executing the above UPDATE command, our data should like this:
</p>
        <table border="1">
          <tbody>
            <tr>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>CustID </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>FirstName </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>LastName </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>StreetAddress </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>City </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>State </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>ZipCode </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>TotalSales</strong>
                </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">1</font>
              </td>
              <td>
                <font size="1">Steve</font>
              </td>
              <td>
                <font size="1">Smith</font>
              </td>
              <td>
                <font size="1">900 Belle St</font>
              </td>
              <td>
                <font size="1">Detroit</font>
              </td>
              <td>
                <font size="1">MI</font>
              </td>
              <td>
                <font size="1">48888</font>
              </td>
              <td>
                <font size="1">5000.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">2</font>
              </td>
              <td>
                <font size="1">Ryan</font>
              </td>
              <td>
                <font size="1">Miller</font>
              </td>
              <td>
                <font size="1">1 Shutout Ct</font>
              </td>
              <td>
                <font size="1">Buffalo</font>
              </td>
              <td>
                <font size="1">NY</font>
              </td>
              <td>
                <font size="1">32323</font>
              </td>
              <td>
                <font size="1">250.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">3</font>
              </td>
              <td>
                <font size="1">Brad</font>
              </td>
              <td>
                <font size="1">Van Pelt</font>
              </td>
              <td>
                <font size="1">100 Safety St</font>
              </td>
              <td>
                <font size="1">New York</font>
              </td>
              <td>
                <font size="1">NY</font>
              </td>
              <td>
                <font size="1">01111</font>
              </td>
              <td>
                <font size="1">4000.00</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
We use the DELETE command to delete rows in a SQL Server table. The DELETE syntax
is 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DELETE</span> Customer
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> CustID
= 3 </span>
        </pre>
        <p>
The following code will delete Customer 3 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DELETE</span> Customer
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> CustID
= 3 </span>
        </pre>
        <p>
After executing the above DELETE command, our data will look like this: 
</p>
        <table border="1">
          <tbody>
            <tr>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>CustID </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>FirstName </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>LastName </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>StreetAddress </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>City </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>State </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>ZipCode </strong>
                </font>
              </td>
              <td style="COLOR: black; BACKGROUND-COLOR: white">
                <font size="1">
                  <strong>TotalSales</strong>
                </font>
              </td>
            </tr>
            <tr>
              <td>
                <font color="#000000" size="1">1</font>
              </td>
              <td>
                <font color="#000000" size="1">Steve</font>
              </td>
              <td>
                <font color="#000000" size="1">Smith</font>
              </td>
              <td>
                <font color="#000000" size="1">900 Belle St</font>
              </td>
              <td>
                <font color="#000000" size="1">Detroit</font>
              </td>
              <td>
                <font color="#000000" size="1">MI</font>
              </td>
              <td>
                <font color="#000000" size="1">48888</font>
              </td>
              <td>
                <font color="#000000" size="1">5000.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font color="#000000" size="1">2</font>
              </td>
              <td>
                <font color="#000000" size="1">Ryan</font>
              </td>
              <td>
                <font color="#000000" size="1">Miller</font>
              </td>
              <td>
                <font color="#000000" size="1">1 Shutout Ct</font>
              </td>
              <td>
                <font color="#000000" size="1">Buffalo</font>
              </td>
              <td>
                <font color="#000000" size="1">NY</font>
              </td>
              <td>
                <font color="#000000" size="1">32323</font>
              </td>
              <td>
                <font color="#000000" size="1">250.00</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
In this article, we showed how to use the INSERT, UPDATE and DELETE commands to modify
data in a SQL Server table. 
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=6257f4a8-134a-4f18-9372-7c38ed4c7219" />
      </body>
      <title>SQL 101 - Part 6: Inserts, Updates and Deletes</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,6257f4a8-134a-4f18-9372-7c38ed4c7219.aspx</guid>
      <link>http://www.davidgiard.com/2009/03/27/SQL101Part6InsertsUpdatesAndDeletes.aspx</link>
      <pubDate>Fri, 27 Mar 2009 04:18:46 GMT</pubDate>
      <description>&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;div style="BORDER-RIGHT: thin solid; BORDER-TOP: thin solid; MARGIN: 5px; BORDER-LEFT: thin solid; WIDTH: 80%; BORDER-BOTTOM: thin solid; BACKGROUND-COLOR: silver"&gt;
&lt;p&gt;
NOTE: 
&lt;/p&gt;
&lt;p&gt;
For demos in this article, we will use a table named Customer that contains the following
columns: 
&lt;/p&gt;
&lt;table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Name &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Data Type &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;FirstName&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(50)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;LastName&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(50)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;StreetAddress&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(255)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;City&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(255)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;State&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;char(2)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;ZipCode&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(10)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;TotalSales&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;decimal(18,2)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
In addition, I created a primary key on the CustID column and set it to autoincrement
by setting the following properties: 
&lt;/p&gt;
&lt;table style="MARGIN-LEFT: 40px" border=0&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
Is Identity 
&lt;/td&gt;
&lt;td&gt;
Yes 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
Identity Seed 
&lt;/td&gt;
&lt;td&gt;
1 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
Identity Seed 
&lt;/td&gt;
&lt;td&gt;
1 
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
Afer adding a couple rows to the table, the data looks like this. 
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;CustID &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;StreetAddress &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;State &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;TotalSales &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Ryan&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Miller&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1 Shutout Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Buffalo&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;NY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;32323&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;250.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;
We can use the INSERT command to add a new row to this table. The syntax of the INSERT
command is 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INSERT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INTO&lt;/span&gt; [TableName]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;( &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[Column List] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;VALUES&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[Values List] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;) &lt;/span&gt; &lt;/pre&gt;
&lt;p&gt;
We can insert a row for a new Customer - Brad Van Pelt - with the following code. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INSERT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INTO&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;( &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StreetAddress, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;City, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ZipCode, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TotalSales &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;VALUES&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'Brad'&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'Van
Pelt'&lt;/span&gt;, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'99
Linebaker Ln'&lt;/span&gt;, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'Owosso'&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'MI'&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'47777'&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4000 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;) &lt;/span&gt; &lt;/pre&gt;
&lt;p&gt;
Notice that each column name in the first set of parentheses matches a value in the
second set of parentheses: 'Brad' with &lt;i&gt;FirstName&lt;/i&gt;, 'Van Pelt' with &lt;i&gt;LastName&lt;/i&gt; and
so on. Notice also that we did not provide a value for the &lt;i&gt;CustID&lt;/i&gt; column. This
is because &lt;i&gt;CustID&lt;/i&gt; is an identity column and, therefore, gets populated with
an incremented number when a new row is added. 
&lt;/p&gt;
&lt;p&gt;
After executing the above INSERT statement, our data should look like this: 
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;CustID &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;StreetAddress &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;State &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;TotalSales &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Ryan&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Miller&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1 Shutout Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Buffalo&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;NY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;32323&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;250.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Brad&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Van Pelt&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;99 Linebaker Ln&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Owosso&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;47777&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;4000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
In this article, we showed how to use T-SQL's INSERT, UPDATE, and DELETE commands
to modify the data in a table. 
&lt;/p&gt;
&lt;p&gt;
The new customer was automatically assigned a &lt;i&gt;CustID&lt;/i&gt; value of 3. Because this
value uniquely identifies the newly-added row, we can use it to find and update that
row. The syntax to update a row in SQL Server is 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;UPDATE&lt;/span&gt; [Table]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; [Column1]
= [New Value 1], &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[Column2] = [New
Value 2], &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[Column3] = [New Value 3],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;etc... &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; [Filter
Condition] &lt;/span&gt; &lt;/pre&gt;
&lt;p&gt;
Only rows that match the filter condition will be updated and only those columns specified
in the SET clause will be updated.&amp;nbsp; We will use the following command to update
the &lt;i&gt;StreetAddress&lt;/i&gt;, &lt;i&gt;City&lt;/i&gt;, &lt;i&gt;State&lt;/i&gt; and &lt;i&gt;ZipCdoe&lt;/i&gt; columns of
Customer 3:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;UPDATE&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; StreetAddress
= &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'100
Safety St'&lt;/span&gt;, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;City=&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'New
York'&lt;/span&gt;, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State=&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'NY'&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ZipCode=&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'01111'&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; CustID
= 3 &lt;/span&gt; &lt;/pre&gt;
&lt;p&gt;
After executing the above UPDATE command, our data should like this:
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;CustID &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;FirstName &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;LastName &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;StreetAddress &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;City &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;State &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;ZipCode &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;TotalSales&lt;/strong&gt; &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Ryan&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Miller&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1 Shutout Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Buffalo&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;NY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;32323&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;250.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Brad&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Van Pelt&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;100 Safety St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;New York&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;NY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;01111&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;4000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
We use the DELETE command to delete rows in a SQL Server table. The DELETE syntax
is 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DELETE&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; CustID
= 3 &lt;/span&gt; &lt;/pre&gt;
&lt;p&gt;
The following code will delete Customer 3 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DELETE&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; CustID
= 3 &lt;/span&gt; &lt;/pre&gt;
&lt;p&gt;
After executing the above DELETE command, our data will look like this: 
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;CustID &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;FirstName &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;LastName &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;StreetAddress &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;City &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;State &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;ZipCode &lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;&lt;strong&gt;TotalSales&lt;/strong&gt; &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;Ryan&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;Miller&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;1 Shutout Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;Buffalo&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;NY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;32323&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font color=#000000 size=1&gt;250.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
In this article, we showed how to use the INSERT, UPDATE and DELETE commands to modify
data in a SQL Server table. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=6257f4a8-134a-4f18-9372-7c38ed4c7219" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,6257f4a8-134a-4f18-9372-7c38ed4c7219.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=75aef884-d213-4aa0-93e4-d045721d876e</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,75aef884-d213-4aa0-93e4-d045721d876e.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,75aef884-d213-4aa0-93e4-d045721d876e.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=75aef884-d213-4aa0-93e4-d045721d876e</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        <div style="BORDER-RIGHT: thin solid; BORDER-TOP: thin solid; MARGIN: 5px; BORDER-LEFT: thin solid; WIDTH: 80%; BORDER-BOTTOM: thin solid; BACKGROUND-COLOR: silver">
          <p>
NOTE: 
</p>
          <p>
For demos in this article, we will use three tables: Customer, SalesOrder and OrderLine.  
</p>
          <p>
The structure of the Customer table is:
</p>
          <table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border="1">
            <tbody>
              <tr>
                <td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left">
                  <font size="1">Name </font>
                </td>
                <td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left">
                  <font size="1">Data Type </font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">CustID</font>
                </td>
                <td>
                  <font size="1">int</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">FirstName</font>
                </td>
                <td>
                  <font size="1">nvarchar(50)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">LastName</font>
                </td>
                <td>
                  <font size="1">nvarchar(50)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">StreetAddress</font>
                </td>
                <td>
                  <font size="1">nvarchar(255)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">City </font>
                </td>
                <td>
                  <font size="1">nvarchar(255) </font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">State</font>
                </td>
                <td>
                  <font size="1">char(2)</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">ZipCode </font>
                </td>
                <td>
                  <font size="1">nvarchar(10)</font>
                </td>
              </tr>
            </tbody>
          </table>
          <p>
The Customer table contains the following data. 
</p>
          <table border="1">
            <tbody>
              <tr>
                <td class="style1" style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">CustID </font>
                </td>
                <td class="style1" style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">FirstName </font>
                </td>
                <td class="style1" style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">LastName </font>
                </td>
                <td class="style1" style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">StreetAddress </font>
                </td>
                <td class="style1" style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">City </font>
                </td>
                <td class="style1" style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">State </font>
                </td>
                <td class="style1" style="COLOR: black; BACKGROUND-COLOR: white">
                  <font size="1">ZipCode </font>
                </td>
              </tr>
              <tr>
                <td class="style1">
                  <font size="1">1</font>
                </td>
                <td class="style1">
                  <font size="1">David</font>
                </td>
                <td class="style1">
                  <font size="1">Giard</font>
                </td>
                <td class="style1">
                  <font size="1">123 Oxford Ct</font>
                </td>
                <td class="style1">
                  <font size="1">Erlanger</font>
                </td>
                <td class="style1">
                  <font size="1">KY</font>
                </td>
                <td class="style1">
                  <font size="1">40111</font>
                </td>
              </tr>
              <tr>
                <td class="style1">
                  <font size="1">2</font>
                </td>
                <td class="style1">
                  <font size="1">Magic</font>
                </td>
                <td class="style1">
                  <font size="1">Johnson</font>
                </td>
                <td class="style1">
                  <font size="1">456 Hollywood Blvd</font>
                </td>
                <td class="style1">
                  <font size="1">Lansing</font>
                </td>
                <td class="style1">
                  <font size="1">MI</font>
                </td>
                <td class="style1">
                  <font size="1">45222</font>
                </td>
              </tr>
              <tr>
                <td class="style1">
                  <font size="1">3</font>
                </td>
                <td class="style1">
                  <font size="1">Bubba</font>
                </td>
                <td class="style1">
                  <font size="1">Smith</font>
                </td>
                <td class="style1">
                  <font size="1">789 Killbubbakill St</font>
                </td>
                <td class="style1">
                  <font size="1">Baltimore</font>
                </td>
                <td class="style1">
                  <font size="1">MD</font>
                </td>
                <td class="style1">
                  <font size="1">10111</font>
                </td>
              </tr>
            </tbody>
          </table>
          <br />
The structure of the SalesOrder table is<br /><table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border="1"><tbody><tr><td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"><font size="1">Name </font></td><td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"><font size="1">Data Type </font></td></tr><tr><td><font size="1">OrderID</font></td><td><font size="1">int</font></td></tr><tr><td><font size="1">CustID</font></td><td><font size="1">int</font></td></tr><tr><td><font size="1">OrderDate</font></td><td><font size="1">datetime</font></td></tr></tbody></table><p>
The SalesOrder table contains the following data. 
</p><table border="1"><tbody><tr><td class="style1" style="COLOR: black; BACKGROUND-COLOR: white"><font size="1">OrderID</font></td><td class="style1" style="COLOR: black; BACKGROUND-COLOR: white"><font size="1">CustID </font></td><td class="style1" style="COLOR: black; BACKGROUND-COLOR: white"><font size="1">OrderDate</font></td></tr><tr><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">2009-03-01</font></td></tr><tr><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">2009-03-02</font></td></tr><tr><td class="style1"><font size="1">3</font></td><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">2009-03-07</font></td></tr><tr><td class="style1"><font size="1">4</font></td><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">2009-03-14</font></td></tr><tr><td class="style1"><font size="1">5</font></td><td class="style1"><font size="1">3</font></td><td class="style1"><font size="1">2009-03-21</font></td></tr></tbody></table><br />
The structure of the OrderLine table is<br /><table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border="1"><tbody><tr><td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"><font size="1">Name </font></td><td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"><font size="1">Data Type </font></td></tr><tr><td><font size="1">OrderID</font></td><td><font size="1">int</font></td></tr><tr><td><font size="1">LineNumber</font></td><td><font size="1">int</font></td></tr><tr><td><font size="1">ProductName</font></td><td><font size="1">nvarchar(255)</font></td></tr><tr><td><font size="1">Quantity</font></td><td><font size="1">int</font></td></tr></tbody></table><br />
The OrderLine table contains the following data   
<br /><table border="1"><tbody><tr><td class="style1" style="COLOR: black; BACKGROUND-COLOR: white"><font size="1">OrderID</font></td><td class="style1" style="COLOR: black; BACKGROUND-COLOR: white"><font size="1">LineNumber</font></td><td class="style1" style="COLOR: black; BACKGROUND-COLOR: white"><font size="1">ProductName</font></td><td class="style1" style="COLOR: black; BACKGROUND-COLOR: white"><font size="1">Quantity</font></td></tr><tr><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">Widget</font></td><td class="style1"><font size="1">7</font></td></tr><tr><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">Super Widget</font></td><td class="style1"><font size="1">4</font></td></tr><tr><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">Widget</font></td><td class="style1"><font size="1">5</font></td></tr><tr><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">Super Widget</font></td><td class="style1"><font size="1">3</font></td></tr><tr><td class="style1"><font size="1">3</font></td><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">Widget</font></td><td class="style1"><font size="1">2</font></td></tr><tr><td class="style1"><font size="1">4</font></td><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">Super Widget</font></td><td class="style1"><font size="1">3</font></td></tr><tr><td class="style1"><font size="1">5</font></td><td class="style1"><font size="1">1</font></td><td class="style1"><font size="1">Widget</font></td><td class="style1"><font size="1">6</font></td></tr><tr><td class="style1"><font size="1">5</font></td><td class="style1"><font size="1">2</font></td><td class="style1"><font size="1">Super Widget</font></td><td class="style1"><font size="1">1</font></td></tr></tbody></table><br /></div>
        <p>
In a previous article, I explained how we can (and often should) split a table into
multiple tables in order to eliminate data redundancy - a process known as "normalization".
</p>
        <p>
In this article, I'll explain how to retrieve related data from multiple tables and
return them in a single result set.
</p>
        <p>
Recall from the Normalization article that - in order to relate to tables - we add
a key to each table.  The Primary key in the parent table is a column that is
unique for each row and, therefore, servers to uniquely identify a row.  The
child table contains a foreign key which is the same value as a Primary key in the
parent table, so it points to a given row in the parent.
</p>
        <p>
To retrieve data from multiple tables into a single result set, we do something called
a "JOIN".  In SQL, there are two ways to JOIN tables:
</p>
        <ul>
          <li>
Using the JOIN keyword 
</li>
          <li>
Adding the join condition on a WHERE clause</li>
        </ul>
        <h3>JOIN keyword
</h3>
        <p>
The syntax for joining tables with the JOIN keyword is
</p>
        <p>
The syntax for joining tables with the JOIN keyword is
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT
[List of Columns]<br />
    FROM [Table 1] 
<br />
    JOIN [Table 2]<br />
        ON [Join Condition]</span>
          </span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> </span>
          </span>For
example, to retrieve the Name, SalesOrder Date and SalesOrder Amount of each customer
in our sample tables, use the following query:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span>         FirstName,
        LastName,         OrderDate
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Customer
    <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> SalesOrder
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> Customer.CustID
= SalesOrder.CustID</span>
        </pre>
        <p>
Notice that we need to prefix the CustID column name with the table name in our filter
condition.  This is because the CustID column name is not unique.  We need
to tell SQL to which column we are referring.
</p>
        <p>
The results of this query are
</p>
        <table style="FONT-SIZE: x-small" border="1">
          <tbody>
            <tr style="FONT-SIZE: x-small">
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">FirstName </font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">LastName </font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">OrderDate</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-01</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-02</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-07</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">Magic</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Johnson</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-14</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">Bubba</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Smith</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-21</font>
              </td>
            </tr>
          </tbody>
        </table>
        <h3>WHERE clause
</h3>
        <p>
The syntax for joining two tables with the WHERE clause is
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT
[List of Columns]<br />
    FROM [Table 1], [Table 2] 
<br />
    WHERE [Join Condition] </span>
          </span>
        </p>
        <p>
The syntax to return the same result set as above is
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span>
            <br />
        FirstName, 
<br />
        LastName, 
<br />
        OrderDate 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Customer,
SalesOrder 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> Customer.CustID
= SalesOrder.CustID</span>
        </p>
        <p>
Recall that the WHERE clause is also used to filter your result set.  In fact,
you can use it for both filtering and joining.  The following two queries yield
the same results (showing only those records that match customer 1.
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span>
            <br />
        FirstName, 
<br />
        LastName, 
<br />
        OrderDate 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Customer 
<br />
    <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> SalesOrder 
<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> Customer.CustID
= SalesOrder.CustID 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> Customer.CustID
= 1</span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span>
            <br />
        FirstName, 
<br />
        LastName, 
<br />
        OrderDate 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Customer,
SalesOrder 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> Customer.CustID
= SalesOrder.CustID 
<br />
        <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AND</span> Customer.CustID
= 1</span>
        </p>
        <p>
Here is the result set for either of the above two queries:
</p>
        <table style="FONT-SIZE: x-small" border="1">
          <tbody>
            <tr style="FONT-SIZE: x-small">
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">FirstName </font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">LastName </font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">OrderDate</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-01</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-02</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-07</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
You can use these same techniques to join more than two tables. Here is the syntax
to add the OrderLine table to our queries
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span>
            <br />
        SalesOrder.OrderID, 
<br />
        FirstName, 
<br />
        LastName, 
<br />
        OrderDate,<br />
        ProductName,<br />
        Quantity<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Customer 
<br />
    <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> SalesOrder 
<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> Customer.CustID
= SalesOrder.CustID 
<br />
    <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">JOIN</span> OrderLine 
<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> SalesOrder.OrderID
= OrderLine.OrderID</span>
        </p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span>
            <br />
        SalesOrder.OrderID, 
<br />
        FirstName, 
<br />
        LastName, 
<br />
        OrderDate,<br />
        ProductName,<br />
        Quantity<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> Customer,
SalesOrder, OrderLine<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> Customer.CustID
= SalesOrder.CustID 
<br />
        <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AND</span> SalesOrder.OrderID
= OrderLine.OrderID</span>
        </p>
        <p>
Here is the result set of either of these 3-table queries
</p>
        <table style="FONT-SIZE: x-small" border="1">
          <tbody>
            <tr style="FONT-SIZE: x-small">
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">OrderID</font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">FirstName </font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">LastName </font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">OrderDate</font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">ProductName</font>
              </td>
              <td class="style1" style="COLOR: white; BACKGROUND-COLOR: gray">
                <font size="1">Quantity</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">1</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-01</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">7</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">1</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-01</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Super Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">4</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">2</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-02</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">5</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">2</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-02</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Super Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">3</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">3</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">David</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Giard</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-07</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">4</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Magic</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Johnson</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-14</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Super Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">3</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">5</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Bubba</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Smith</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-21</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">6</font>
              </td>
            </tr>
            <tr>
              <td class="style1">
                <font color="#000000" size="1">5</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Bubba</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Smith</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">2009-03-21</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">Super Widget</font>
              </td>
              <td class="style1">
                <font color="#000000" size="1">1</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
I prefer to use the JOIN keyword syntax when joining tables together because it is
more clear what part of the query is a filter and what part of a query is a join.  
</p>
        <p>
In this article, we showed the ways to use SQL Server to join multiple tables into
a single result set.
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=75aef884-d213-4aa0-93e4-d045721d876e" />
      </body>
      <title>SQL 101 - Part 5: Joins</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,75aef884-d213-4aa0-93e4-d045721d876e.aspx</guid>
      <link>http://www.davidgiard.com/2009/03/21/SQL101Part5Joins.aspx</link>
      <pubDate>Sat, 21 Mar 2009 22:31:36 GMT</pubDate>
      <description>&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;div style="BORDER-RIGHT: thin solid; BORDER-TOP: thin solid; MARGIN: 5px; BORDER-LEFT: thin solid; WIDTH: 80%; BORDER-BOTTOM: thin solid; BACKGROUND-COLOR: silver"&gt;
&lt;p&gt;
NOTE: 
&lt;/p&gt;
&lt;p&gt;
For demos in this article, we will use three tables: Customer, SalesOrder and OrderLine.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
The structure of the Customer table is:
&lt;/p&gt;
&lt;table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Name &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Data Type &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;CustID&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;int&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;FirstName&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(50)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;LastName&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(50)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;StreetAddress&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(255)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(255) &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;State&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;char(2)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(10)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
The Customer table contains the following data. 
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;CustID &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;StreetAddress &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;State &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;123 Oxford Ct&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Erlanger&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;KY&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;40111&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;456 Hollywood Blvd&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Lansing&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;45222&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;789 Killbubbakill St&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Baltimore&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;MD&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;10111&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
The structure of the SalesOrder table is&lt;br&gt;
&lt;table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Name &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Data Type &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;OrderID&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;int&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;CustID&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;int&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;OrderDate&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;datetime&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
The SalesOrder table contains the following data. 
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;OrderID&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;CustID &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;OrderDate&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2009-03-01&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2009-03-02&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2009-03-07&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2009-03-14&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2009-03-21&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
The structure of the OrderLine table is&lt;br&gt;
&lt;table style="BORDER-RIGHT: white thin solid; BORDER-TOP: white thin solid; BORDER-LEFT: white thin solid; WIDTH: 57%; COLOR: white; BORDER-BOTTOM: white thin solid; BACKGROUND-COLOR: gray; TEXT-ALIGN: left" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Name &lt;/font&gt;&lt;/td&gt;
&lt;td style="COLOR: black; BACKGROUND-COLOR: white; TEXT-ALIGN: left"&gt;
&lt;font size=1&gt;Data Type &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;OrderID&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;int&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;LineNumber&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;int&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;ProductName&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;nvarchar(255)&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Quantity&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;int&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
The OrderLine table contains the following data&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;OrderID&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;LineNumber&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;ProductName&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: black; BACKGROUND-COLOR: white"&gt;
&lt;font size=1&gt;Quantity&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;7&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;6&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;p&gt;
In a previous article, I explained how we can (and often should) split a table into
multiple tables in order to eliminate data redundancy - a process known as "normalization".
&lt;/p&gt;
&lt;p&gt;
In this article, I'll explain how to retrieve related data from multiple tables and
return them in a single result set.
&lt;/p&gt;
&lt;p&gt;
Recall from the Normalization article that - in order to relate to tables - we add
a key to each table.&amp;nbsp; The Primary key in the parent table is a column that is
unique for each row and, therefore, servers to uniquely identify a row.&amp;nbsp; The
child table contains a foreign key which is the same value as a Primary key in the
parent table, so it points to a given row in the parent.
&lt;/p&gt;
&lt;p&gt;
To retrieve data from multiple tables into a single result set, we do something called
a "JOIN".&amp;nbsp; In SQL, there are two ways to JOIN tables:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Using the JOIN keyword 
&lt;li&gt;
Adding the join condition on a WHERE clause&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;JOIN keyword
&lt;/h3&gt;
&lt;p&gt;
The syntax for joining tables with the JOIN keyword is
&lt;/p&gt;
&lt;p&gt;
The syntax for joining tables with the JOIN keyword is
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT
[List of Columns]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM [Table 1] 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;JOIN [Table 2]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON [Join Condition]&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;For
example, to retrieve the Name, SalesOrder Date and SalesOrder Amount of each customer
in our sample tables, use the following query:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OrderDate
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; SalesOrder
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; Customer.CustID
= SalesOrder.CustID&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Notice that we need to prefix the CustID column name with the table name in our filter
condition.&amp;nbsp; This is because the CustID column name is not unique.&amp;nbsp; We need
to tell SQL to which column we are referring.
&lt;/p&gt;
&lt;p&gt;
The results of this query are
&lt;/p&gt;
&lt;table style="FONT-SIZE: x-small" border=1&gt;
&lt;tbody&gt;
&lt;tr style="FONT-SIZE: x-small"&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;OrderDate&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-01&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-02&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-07&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-14&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-21&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3&gt;WHERE clause
&lt;/h3&gt;
&lt;p&gt;
The syntax for joining two tables with the WHERE clause is
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT
[List of Columns]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM [Table 1], [Table 2] 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE [Join Condition] &lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
The syntax to return the same result set as above is
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OrderDate 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Customer,
SalesOrder 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; Customer.CustID
= SalesOrder.CustID&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Recall that the WHERE clause is also used to filter your result set.&amp;nbsp; In fact,
you can use it for both filtering and joining.&amp;nbsp; The following two queries yield
the same results (showing only those records that match customer 1.
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OrderDate 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Customer 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; SalesOrder 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; Customer.CustID
= SalesOrder.CustID 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; Customer.CustID
= 1&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OrderDate 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Customer,
SalesOrder 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; Customer.CustID
= SalesOrder.CustID 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AND&lt;/span&gt; Customer.CustID
= 1&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Here is the result set for either of the above two queries:
&lt;/p&gt;
&lt;table style="FONT-SIZE: x-small" border=1&gt;
&lt;tbody&gt;
&lt;tr style="FONT-SIZE: x-small"&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;OrderDate&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-01&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-02&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-07&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
You can use these same techniques to join more than two tables. Here is the syntax
to add the OrderLine table to our queries
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SalesOrder.OrderID, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OrderDate,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ProductName,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Quantity&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Customer 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; SalesOrder 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; Customer.CustID
= SalesOrder.CustID 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;JOIN&lt;/span&gt; OrderLine 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; SalesOrder.OrderID
= OrderLine.OrderID&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SalesOrder.OrderID, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OrderDate,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ProductName,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Quantity&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; Customer,
SalesOrder, OrderLine&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; Customer.CustID
= SalesOrder.CustID 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AND&lt;/span&gt; SalesOrder.OrderID
= OrderLine.OrderID&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Here is the result set of either of these 3-table queries
&lt;/p&gt;
&lt;table style="FONT-SIZE: x-small" border=1&gt;
&lt;tbody&gt;
&lt;tr style="FONT-SIZE: x-small"&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;OrderID&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;OrderDate&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;ProductName&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1 style="COLOR: white; BACKGROUND-COLOR: gray"&gt;
&lt;font size=1&gt;Quantity&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-01&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;7&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-01&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-02&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-02&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-07&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-14&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-21&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;6&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;2009-03-21&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;Super Widget&lt;/font&gt;&lt;/td&gt;
&lt;td class=style1&gt;
&lt;font color=#000000 size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
I prefer to use the JOIN keyword syntax when joining tables together because it is
more clear what part of the query is a filter and what part of a query is a join.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
In this article, we showed the ways to use SQL Server to join multiple tables into
a single result set.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=75aef884-d213-4aa0-93e4-d045721d876e" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,75aef884-d213-4aa0-93e4-d045721d876e.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=2c88dcbd-fcb2-41a0-8b77-e51c8941d429</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,2c88dcbd-fcb2-41a0-8b77-e51c8941d429.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,2c88dcbd-fcb2-41a0-8b77-e51c8941d429.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=2c88dcbd-fcb2-41a0-8b77-e51c8941d429</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        <div style="BORDER-BOTTOM: thin solid; BORDER-LEFT: thin solid; BACKGROUND-COLOR: silver; MARGIN: 5px; WIDTH: 80%; BORDER-TOP: thin solid; BORDER-RIGHT: thin solid">
          <p>
NOTE: 
</p>
          <p>
For demos in this article, we will use a table named Customer that contains 7 columns: 
</p>
          <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border="1">
            <tbody>
              <tr>
                <td style="TEXT-ALIGN: left; BACKGROUND-COLOR: white; COLOR: black">
Name 
</td>
                <td style="TEXT-ALIGN: left; BACKGROUND-COLOR: white; COLOR: black">
Data Type 
</td>
              </tr>
              <tr>
                <td>
FirstName</td>
                <td>
nvarchar(50)</td>
              </tr>
              <tr>
                <td>
LastName</td>
                <td>
nvarchar(50)</td>
              </tr>
              <tr>
                <td>
StreetAddress</td>
                <td>
nvarchar(255)</td>
              </tr>
              <tr>
                <td>
City</td>
                <td>
nvarchar(255)</td>
              </tr>
              <tr>
                <td>
State</td>
                <td>
char(2)</td>
              </tr>
              <tr>
                <td>
ZipCode</td>
                <td>
nvarchar(10)</td>
              </tr>
              <tr>
                <td>
TotalSales</td>
                <td>
money</td>
              </tr>
            </tbody>
          </table>
          <p>
Afer adding a few rows to the table, the data looks like this. 
</p>
          <table border="1">
            <tbody>
              <tr>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">CustID </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">FirstName </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">LastName </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">StreetAddress </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">City </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">State </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">ZipCode </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="2">TotalSales </font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="2">1</font>
                </td>
                <td>
                  <font size="2">David</font>
                </td>
                <td>
                  <font size="2">Giard</font>
                </td>
                <td>
                  <font size="2">123 Oxford Ct</font>
                </td>
                <td>
                  <font size="2">Erlanger</font>
                </td>
                <td>
                  <font size="2">KY</font>
                </td>
                <td>
                  <font size="2">40111</font>
                </td>
                <td>
                  <font size="2">500.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="2">2</font>
                </td>
                <td>
                  <font size="2">Magic</font>
                </td>
                <td>
                  <font size="2">Johnson</font>
                </td>
                <td>
                  <font size="2">456 Hollywood Blvd</font>
                </td>
                <td>
                  <font size="2">Lansing</font>
                </td>
                <td>
                  <font size="2">MI</font>
                </td>
                <td>
                  <font size="2">45222</font>
                </td>
                <td>
                  <font size="2">1500.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="2">3</font>
                </td>
                <td>
                  <font size="2">Bubba</font>
                </td>
                <td>
                  <font size="2">Smith</font>
                </td>
                <td>
                  <font size="2">789 Killbubbakill St</font>
                </td>
                <td>
                  <font size="2">Baltimore</font>
                </td>
                <td>
                  <font size="2">MD</font>
                </td>
                <td>
                  <font size="2">10111</font>
                </td>
                <td>
                  <font size="2">1000.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="2">4</font>
                </td>
                <td>
                  <font size="2">Ron</font>
                </td>
                <td>
                  <font size="2">Mason</font>
                </td>
                <td>
                  <font size="2">501 E Grand River Ave</font>
                </td>
                <td>
                  <font size="2">Lansing</font>
                </td>
                <td>
                  <font size="2">MI</font>
                </td>
                <td>
                  <font size="2">45333</font>
                </td>
                <td>
                  <font size="2">2000.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="2">5</font>
                </td>
                <td>
                  <font size="2">Steve</font>
                </td>
                <td>
                  <font size="2">Smith</font>
                </td>
                <td>
                  <font size="2">900 Belle St</font>
                </td>
                <td>
                  <font size="2">Detroit</font>
                </td>
                <td>
                  <font size="2">MI</font>
                </td>
                <td>
                  <font size="2">48888</font>
                </td>
                <td>
                  <font size="2">5000.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="2">6</font>
                </td>
                <td>
                  <font size="2">Ryan</font>
                </td>
                <td>
                  <font size="2">Miller</font>
                </td>
                <td>
                  <font size="2">1 Shutout Ct</font>
                </td>
                <td>
                  <font size="2">Buffalo</font>
                </td>
                <td>
                  <font size="2">NY</font>
                </td>
                <td>
                  <font size="2">32323</font>
                </td>
                <td>
                  <font size="2">250.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="2">7</font>
                </td>
                <td>
                  <font size="2">Brad</font>
                </td>
                <td>
                  <font size="2">Van Pelt</font>
                </td>
                <td>
                  <font size="2">99 Linebaker Ln</font>
                </td>
                <td>
                  <font size="2">Owosso</font>
                </td>
                <td>
                  <font size="2">MI</font>
                </td>
                <td>
                  <font size="2">47777</font>
                </td>
                <td>
                  <font size="2">4000.00</font>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <p>
In the last article in this series, I expleined the basic functionality of the SELECT
statement. I showed how to select columns from a table and sort or filter the results. 
</p>
        <p>
In this article, I'll show some more things you can do with the SELECT statemnts. 
</p>
        <p>
Sometimes we want our results to aggregate data. When aggregating data, we use functions
that consolidate multiple rows and return the result of this aggregate function applied
to many rows. The most common aggregate functions I use that SQL Server supports are: 
</p>
        <table border="1">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font size="2">Function </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font size="2">Descripiton</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="2">MAX </font>
              </td>
              <td>
                <font size="2">The maximum value of a column across all included rows </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="2">MIN </font>
              </td>
              <td>
                <font size="2">The minimum value of a column across all included rows </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="2">SUM </font>
              </td>
              <td>
                <font size="2">The sum of all values in a column across all included rows </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="2">AVG </font>
              </td>
              <td>
                <font size="2">The arithmetic average of all values in a column across all included
rows </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="2">COUNT </font>
              </td>
              <td>
                <font size="2">The number of included rows</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
The MIN, MAX, SUM, and AVG functions accept a parameter - the name of the column on
which to calculate these values. The column must hold a numeric data type, such as
an INT, FLOAT or MONEY. 
</p>
        <p>
You may pass a column name as a parameter to the COUNT function but it doesn't matter
which column because - in SQL Server - every column appears exactly once in each row
and the COUNT function is used to count rows. By convention, we pass "*" as the parameter
to the COUNT aggregate function to represent all rows. 
</p>
        <p>
A few examples will help clarify this. Since our table contains only one numeric column
- <i>TotalSales</i> - we will use this in most of our aggregate functions. 
</p>
        <p>
The following query returns the maximum value of the <i>TotalSales</i> column. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">MAX</span> (TotalSales) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> MaxSales
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer </span>
        </pre>
        <p>
Here are the results - one row with one column containing the highest numerical value
in the <i>TotalSales</i> column. 
</p>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border="1">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
MaxSales</td>
            </tr>
            <tr>
              <td align="middle">
5000.00</td>
            </tr>
          </tbody>
        </table>
        <p>
Similarly, the following query returns the sum of all values in the <i>TotalSales</i> column. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">SUM</span> (TotalSales) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> SumSales
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer </span>
        </pre>
        <p>
This returns 14,750 which is the sum of 1000 + 1500 + 1000 + 2000 + 5000 + 250 + 4000 
</p>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border="1">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
SumSales</td>
            </tr>
            <tr>
              <td align="middle">
14750.00</td>
            </tr>
          </tbody>
        </table>
        <p>
We can filter before applying an aggregate function. If we only want to sum of <i>TotalSales</i> for
customers in michigan, we simply add a WHERE clause to our query. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">SUM</span> (TotalSales) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> SumSales
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">WHERE</span> State
= <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">'MI'</span></span>
        </pre>
        <p>
This returns 12,500, which is 1500 + 2000 + 5000 + 4000, or the sum of the <i>TotalSales</i> column
for only those customers in Michigan. 
</p>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border="1">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
SumSales</td>
            </tr>
            <tr>
              <td align="middle">
12500.00</td>
            </tr>
          </tbody>
        </table>
        <p>
As mentioned before, we do not need to specify a particular column for the COUNT function. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">COUNT</span> (*) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> NumCusts
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer </span>
        </pre>
        <p>
returns the a row and column containing number 7, which is how many rows are in our
table. 
</p>
        <p>
So far, each query we have written has returned only one row. Often, however, we want
to calculate an aggregation for each distinct value in a column or columns. We can
do this by adding a GROUP BY clause to the query. For example, we may want to see
the SUM of <i>TotalSales</i> for each state. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>         State,
        <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">SUM</span> (TotalSales) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> SumSales
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">GROUP</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">BY</span> State </span>
        </pre>
        <p>
The above query returns a row for each distinct value in the State column and calculates
the sum of <i>TotalSales</i> of all rows corresponding to that state.
</p>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border="1">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
State</td>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
SumSales</td>
            </tr>
            <tr>
              <td align="middle">
 KY</td>
              <td align="middle">
500.00</td>
            </tr>
            <tr>
              <td align="middle">
MD</td>
              <td align="middle">
1000.00</td>
            </tr>
            <tr>
              <td align="middle">
MI</td>
              <td align="middle">
12500.00</td>
            </tr>
            <tr>
              <td align="middle">
NY</td>
              <td align="middle">
500.00</td>
            </tr>
          </tbody>
        </table>
        <p>
It's important to note that, when using the GROUP BY clause, you cannot return a column
that is not part of the grouping. So 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>         State,
        City,         <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">SUM</span> (TotalSales) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> SumSales
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">GROUP</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">BY</span> State </span>
        </pre>
        <p>
results in an error because we are trying to return the city column, but we are not
grouping on that column. Because a given state can have multiple cities, SQL does
not know which one to display for the row returned. 
</p>
        <p>
You can group on multiple columns as in the following query 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>         State,
        City,         <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">SUM</span> (TotalSales) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> SumSales
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">GROUP</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">BY</span> State,
City </span>
        </pre>
        <p>
In this case, we get a row with a sum for each combination of state and city. 
</p>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border="1">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
State</td>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
City</td>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
SumSales</td>
            </tr>
            <tr>
              <td align="middle">
 KY</td>
              <td align="middle">
Erlanger</td>
              <td align="middle">
500.00</td>
            </tr>
            <tr>
              <td align="middle">
MD</td>
              <td align="middle">
Baltimore</td>
              <td align="middle">
1000.00</td>
            </tr>
            <tr>
              <td align="middle">
MI</td>
              <td align="middle">
Lansing</td>
              <td align="middle">
1500.00</td>
            </tr>
            <tr>
              <td align="middle">
MI</td>
              <td align="middle">
Okemos</td>
              <td align="middle">
2000.00</td>
            </tr>
            <tr>
              <td align="middle">
MI</td>
              <td align="middle">
Owosso</td>
              <td align="middle">
4000.00</td>
            </tr>
            <tr>
              <td align="middle">
NY</td>
              <td align="middle">
Buffalo</td>
              <td align="middle">
250.00</td>
            </tr>
          </tbody>
        </table>
        <p>
By using the GROUP BY clause on a large table, we may end up with so many rows that
it becomes difficult to find relevant data. Sometimes, we are only interested in those
times when the aggregate value exceeds some threshhold. In these cases, it would be
nice to only show aggregate rows that exceed that threshhold. This sounds like a good
place to use a filter. Unfortunately, we cannot use the WHERE clause to accomplish
this task because the WHERE clause filters data before the aggregation. We have to
wait until after calculating the aggregate values becuase it is the aggregate values
on which we want to filter. The HAVING clause is used to filter on aggregate values. 
</p>
        <p>
We can run our query to get the <i>TotalSales</i> sum for each state, but show only
those states that have total sales of more than 2000, using the following query 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>         State,
        <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">SUM</span> (TotalSales) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> SumSales
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">GROUP</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">BY</span> State
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">HAVING</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px">SUM</span> (TotalSales)
&gt;= 1000 </span>
        </pre>
        <p>
In this case, we don't see the row for Kentucky and New York because they had total
sales summing less than $1000 
</p>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border="1">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
State</td>
              <td style="BACKGROUND-COLOR: white; COLOR: black" align="middle">
SumSales</td>
            </tr>
            <tr>
              <td align="middle">
MD</td>
              <td align="middle">
1000.00</td>
            </tr>
            <tr>
              <td align="middle">
MI</td>
              <td align="middle">
112500.00</td>
            </tr>
          </tbody>
        </table>
        <p>
In this article, we showed how to use T-SQL's grouping and aggregate functions to
return summary data from a database. 
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=2c88dcbd-fcb2-41a0-8b77-e51c8941d429" />
      </body>
      <title>SQL 101 - Part 4: Aggregating data</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,2c88dcbd-fcb2-41a0-8b77-e51c8941d429.aspx</guid>
      <link>http://www.davidgiard.com/2009/03/12/SQL101Part4AggregatingData.aspx</link>
      <pubDate>Thu, 12 Mar 2009 14:17:07 GMT</pubDate>
      <description>&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;div style="BORDER-BOTTOM: thin solid; BORDER-LEFT: thin solid; BACKGROUND-COLOR: silver; MARGIN: 5px; WIDTH: 80%; BORDER-TOP: thin solid; BORDER-RIGHT: thin solid"&gt;
&lt;p&gt;
NOTE: 
&lt;/p&gt;
&lt;p&gt;
For demos in this article, we will use a table named Customer that contains 7 columns: 
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="TEXT-ALIGN: left; BACKGROUND-COLOR: white; COLOR: black"&gt;
Name 
&lt;/td&gt;
&lt;td style="TEXT-ALIGN: left; BACKGROUND-COLOR: white; COLOR: black"&gt;
Data Type 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
FirstName&lt;/td&gt;
&lt;td&gt;
nvarchar(50)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
LastName&lt;/td&gt;
&lt;td&gt;
nvarchar(50)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
StreetAddress&lt;/td&gt;
&lt;td&gt;
nvarchar(255)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
City&lt;/td&gt;
&lt;td&gt;
nvarchar(255)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
State&lt;/td&gt;
&lt;td&gt;
char(2)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ZipCode&lt;/td&gt;
&lt;td&gt;
nvarchar(10)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
TotalSales&lt;/td&gt;
&lt;td&gt;
money&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
Afer adding a few rows to the table, the data looks like this. 
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;CustID &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;StreetAddress &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;State &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;TotalSales &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;123 Oxford Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Erlanger&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;KY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;40111&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;500.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;456 Hollywood Blvd&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Lansing&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;45222&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;1500.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;789 Killbubbakill St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Baltimore&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;MD&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;10111&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;1000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;501 E Grand River Ave&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Lansing&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;45333&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;2000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;6&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Ryan&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Miller&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;1 Shutout Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Buffalo&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;NY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;32323&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;250.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;7&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Brad&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Van Pelt&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;99 Linebaker Ln&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;Owosso&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;47777&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;4000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;
In the last article in this series, I expleined the basic functionality of the SELECT
statement. I showed how to select columns from a table and sort or filter the results. 
&lt;/p&gt;
&lt;p&gt;
In this article, I'll show some more things you can do with the SELECT statemnts. 
&lt;/p&gt;
&lt;p&gt;
Sometimes we want our results to aggregate data. When aggregating data, we use functions
that consolidate multiple rows and return the result of this aggregate function applied
to many rows. The most common aggregate functions I use that SQL Server supports are: 
&lt;/p&gt;
&lt;table border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;Function &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=2&gt;Descripiton&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;MAX &lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;The maximum value of a column across all included rows &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;MIN &lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;The minimum value of a column across all included rows &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;SUM &lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;The sum of all values in a column across all included rows &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;AVG &lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;The arithmetic average of all values in a column across all included
rows &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=2&gt;COUNT &lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=2&gt;The number of included rows&lt;/font&gt; 
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
The MIN, MAX, SUM, and AVG functions accept a parameter - the name of the column on
which to calculate these values. The column must hold a numeric data type, such as
an INT, FLOAT or MONEY. 
&lt;/p&gt;
&lt;p&gt;
You may pass a column name as a parameter to the COUNT function but it doesn't matter
which column because - in SQL Server - every column appears exactly once in each row
and the COUNT function is used to count rows. By convention, we pass "*" as the parameter
to the COUNT aggregate function to represent all rows. 
&lt;/p&gt;
&lt;p&gt;
A few examples will help clarify this. Since our table contains only one numeric column
- &lt;i&gt;TotalSales&lt;/i&gt; - we will use this in most of our aggregate functions. 
&lt;/p&gt;
&lt;p&gt;
The following query returns the maximum value of the &lt;i&gt;TotalSales&lt;/i&gt; column. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;MAX&lt;/span&gt; (TotalSales) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; MaxSales
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Here are the results - one row with one column containing the highest numerical value
in the &lt;i&gt;TotalSales&lt;/i&gt; column. 
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
MaxSales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
5000.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
Similarly, the following query returns the sum of all values in the &lt;i&gt;TotalSales&lt;/i&gt; column. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;SUM&lt;/span&gt; (TotalSales) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; SumSales
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
This returns 14,750 which is the sum of 1000 + 1500 + 1000 + 2000 + 5000 + 250 + 4000 
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
SumSales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
14750.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
We can filter before applying an aggregate function. If we only want to sum of &lt;i&gt;TotalSales&lt;/i&gt; for
customers in michigan, we simply add a WHERE clause to our query. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;SUM&lt;/span&gt; (TotalSales) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; SumSales
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;WHERE&lt;/span&gt; State
= &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;'MI'&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
This returns 12,500, which is 1500 + 2000 + 5000 + 4000, or the sum of the &lt;i&gt;TotalSales&lt;/i&gt; column
for only those customers in Michigan. 
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
SumSales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
12500.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
As mentioned before, we do not need to specify a particular column for the COUNT function. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;COUNT&lt;/span&gt; (*) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; NumCusts
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
returns the a row and column containing number 7, which is how many rows are in our
table. 
&lt;/p&gt;
&lt;p&gt;
So far, each query we have written has returned only one row. Often, however, we want
to calculate an aggregation for each distinct value in a column or columns. We can
do this by adding a GROUP BY clause to the query. For example, we may want to see
the SUM of &lt;i&gt;TotalSales&lt;/i&gt; for each state. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;SUM&lt;/span&gt; (TotalSales) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; SumSales
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;GROUP&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;BY&lt;/span&gt; State &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The above query returns a row for each distinct value in the State column and calculates
the sum of &lt;i&gt;TotalSales&lt;/i&gt; of all rows corresponding to that state.
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
State&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
SumSales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
&amp;nbsp;KY&lt;/td&gt;
&lt;td align=middle&gt;
500.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MD&lt;/td&gt;
&lt;td align=middle&gt;
1000.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MI&lt;/td&gt;
&lt;td align=middle&gt;
12500.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
NY&lt;/td&gt;
&lt;td align=middle&gt;
500.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
It's important to note that, when using the GROUP BY clause, you cannot return a column
that is not part of the grouping. So 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;City, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;SUM&lt;/span&gt; (TotalSales) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; SumSales
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;GROUP&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;BY&lt;/span&gt; State &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
results in an error because we are trying to return the city column, but we are not
grouping on that column. Because a given state can have multiple cities, SQL does
not know which one to display for the row returned. 
&lt;/p&gt;
&lt;p&gt;
You can group on multiple columns as in the following query 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;City, &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;SUM&lt;/span&gt; (TotalSales) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; SumSales
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;GROUP&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;BY&lt;/span&gt; State,
City &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In this case, we get a row with a sum for each combination of state and city. 
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
State&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
City&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
SumSales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
&amp;nbsp;KY&lt;/td&gt;
&lt;td align=middle&gt;
Erlanger&lt;/td&gt;
&lt;td align=middle&gt;
500.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MD&lt;/td&gt;
&lt;td align=middle&gt;
Baltimore&lt;/td&gt;
&lt;td align=middle&gt;
1000.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MI&lt;/td&gt;
&lt;td align=middle&gt;
Lansing&lt;/td&gt;
&lt;td align=middle&gt;
1500.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MI&lt;/td&gt;
&lt;td align=middle&gt;
Okemos&lt;/td&gt;
&lt;td align=middle&gt;
2000.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MI&lt;/td&gt;
&lt;td align=middle&gt;
Owosso&lt;/td&gt;
&lt;td align=middle&gt;
4000.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
NY&lt;/td&gt;
&lt;td align=middle&gt;
Buffalo&lt;/td&gt;
&lt;td align=middle&gt;
250.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
By using the GROUP BY clause on a large table, we may end up with so many rows that
it becomes difficult to find relevant data. Sometimes, we are only interested in those
times when the aggregate value exceeds some threshhold. In these cases, it would be
nice to only show aggregate rows that exceed that threshhold. This sounds like a good
place to use a filter. Unfortunately, we cannot use the WHERE clause to accomplish
this task because the WHERE clause filters data before the aggregation. We have to
wait until after calculating the aggregate values becuase it is the aggregate values
on which we want to filter. The HAVING clause is used to filter on aggregate values. 
&lt;/p&gt;
&lt;p&gt;
We can run our query to get the &lt;i&gt;TotalSales&lt;/i&gt; sum for each state, but show only
those states that have total sales of more than 2000, using the following query 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;SUM&lt;/span&gt; (TotalSales) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; SumSales
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;GROUP&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;BY&lt;/span&gt; State
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;HAVING&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: fuchsia; FONT-SIZE: 11px"&gt;SUM&lt;/span&gt; (TotalSales)
&amp;gt;= 1000 &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In this case, we don't see the row for Kentucky and New York because they had total
sales summing less than $1000 
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 150px; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid" border=1&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
State&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black" align=middle&gt;
SumSales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MD&lt;/td&gt;
&lt;td align=middle&gt;
1000.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=middle&gt;
MI&lt;/td&gt;
&lt;td align=middle&gt;
112500.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
In this article, we showed how to use T-SQL's grouping and aggregate functions to
return summary data from a database. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=2c88dcbd-fcb2-41a0-8b77-e51c8941d429" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,2c88dcbd-fcb2-41a0-8b77-e51c8941d429.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=54a5b741-5a68-4d55-bc41-1fab2f33490a</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,54a5b741-5a68-4d55-bc41-1fab2f33490a.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,54a5b741-5a68-4d55-bc41-1fab2f33490a.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=54a5b741-5a68-4d55-bc41-1fab2f33490a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Saturday April 4 at the Robert C. Pew Grand Rapids Campus of Grand Valley State Universityin
Grand Rapids, Shane Jordan and I will be delivering a session on the basics of SQL
Server.  This will cover many of the same topics as the <em>Back To Basics: SQL
101 </em>series I have writton on this blog.
</p>
        <p>
The session is part of the West Michigan .Net University event.  You can get
more informaiton and register at <a href="http://www.dayofdotnet.org/WestMichiganDotNetU/">http://www.dayofdotnet.org/WestMichiganDotNetU/</a></p>
        <p>
          <img alt="West Michigan .Net University" src="http://www.dayofdotnet.org/WestMichiganDotNetU/images/Site-Badge-I.gif" />
        </p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=54a5b741-5a68-4d55-bc41-1fab2f33490a" />
      </body>
      <title>'SQL 101' at West Michigan .Net University</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,54a5b741-5a68-4d55-bc41-1fab2f33490a.aspx</guid>
      <link>http://www.davidgiard.com/2009/03/11/SQL101AtWestMichiganNetUniversity.aspx</link>
      <pubDate>Wed, 11 Mar 2009 14:59:10 GMT</pubDate>
      <description>&lt;p&gt;
Saturday April 4 at the Robert C. Pew Grand Rapids Campus of Grand Valley State Universityin
Grand Rapids, Shane Jordan and I will be delivering a session on the basics of SQL
Server.&amp;nbsp; This will cover many of the same topics as the &lt;em&gt;Back To Basics: SQL
101&amp;nbsp;&lt;/em&gt;series I have writton on this blog.
&lt;/p&gt;
&lt;p&gt;
The session is part of the West Michigan .Net University event.&amp;nbsp; You can get
more informaiton and register at &lt;a href="http://www.dayofdotnet.org/WestMichiganDotNetU/"&gt;http://www.dayofdotnet.org/WestMichiganDotNetU/&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img alt="West Michigan .Net University" src="http://www.dayofdotnet.org/WestMichiganDotNetU/images/Site-Badge-I.gif"&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=54a5b741-5a68-4d55-bc41-1fab2f33490a" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,54a5b741-5a68-4d55-bc41-1fab2f33490a.aspx</comments>
      <category>Back To Basics</category>
      <category>Public Speaking</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=e319538e-1502-490c-870c-1a0bfc912bb7</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,e319538e-1502-490c-870c-1a0bfc912bb7.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,e319538e-1502-490c-870c-1a0bfc912bb7.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e319538e-1502-490c-870c-1a0bfc912bb7</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        <div style="BORDER-BOTTOM: thin solid; BORDER-LEFT: thin solid; BACKGROUND-COLOR: silver; MARGIN: 5px; WIDTH: 80%; BORDER-TOP: thin solid; BORDER-RIGHT: thin solid">
          <p>
NOTE: 
</p>
          <p>
For demos in this article, we will use a table named Customer that contains the
followong columns: 
</p>
          <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid">
            <tbody>
              <tr>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
Name</td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
Data Type</td>
              </tr>
              <tr>
                <td>
FirstName</td>
                <td>
nvarchar(50)</td>
              </tr>
              <tr>
                <td>
LastName</td>
                <td>
nvarchar(50)</td>
              </tr>
              <tr>
                <td>
StreetAddress</td>
                <td>
nvarchar(255)</td>
              </tr>
              <tr>
                <td>
 City</td>
                <td>
nvarchar(255)</td>
              </tr>
              <tr>
                <td>
State</td>
                <td>
char(2)</td>
              </tr>
              <tr>
                <td>
 ZipCode</td>
                <td>
nvarchar(10)</td>
              </tr>
              <tr>
                <td>
TotalSales</td>
                <td>
money</td>
              </tr>
            </tbody>
          </table>
          <p>
Afer adding a few rows to the table, the data looks like this. 
</p>
          <table>
            <tbody>
              <tr>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">CustID </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">FirstName </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">LastName </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">StreetAddress </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">City </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">State </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">ZipCode </font>
                </td>
                <td style="BACKGROUND-COLOR: white; COLOR: black">
                  <font size="1">TotalSales </font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">1</font>
                </td>
                <td>
                  <font size="1">David</font>
                </td>
                <td>
                  <font size="1">Giard</font>
                </td>
                <td>
                  <font size="1">123 Oxford Ct</font>
                </td>
                <td>
                  <font size="1">Erlanger</font>
                </td>
                <td>
                  <font size="1">KY</font>
                </td>
                <td>
                  <font size="1">40111</font>
                </td>
                <td>
                  <font size="1">1000.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">2</font>
                </td>
                <td>
                  <font size="1">Magic</font>
                </td>
                <td>
                  <font size="1">Johnson</font>
                </td>
                <td>
                  <font size="1">456 Hollywood Blvd</font>
                </td>
                <td>
                  <font size="1">Lansing</font>
                </td>
                <td>
                  <font size="1">MI</font>
                </td>
                <td>
                  <font size="1">45222</font>
                </td>
                <td>
                  <font size="1">1500.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">3</font>
                </td>
                <td>
                  <font size="1">Bubba</font>
                </td>
                <td>
                  <font size="1">Smith</font>
                </td>
                <td>
                  <font size="1">789 Killbubbakill St</font>
                </td>
                <td>
                  <font size="1">Baltimore</font>
                </td>
                <td>
                  <font size="1">MD</font>
                </td>
                <td>
                  <font size="1">10111</font>
                </td>
                <td>
                  <font size="1">1000.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">4</font>
                </td>
                <td>
                  <font size="1">Ron</font>
                </td>
                <td>
                  <font size="1">Mason</font>
                </td>
                <td>
                  <font size="1">501 E Grand River Ave</font>
                </td>
                <td>
                  <font size="1">Okemos</font>
                </td>
                <td>
                  <font size="1">MI</font>
                </td>
                <td>
                  <font size="1">45333</font>
                </td>
                <td>
                  <font size="1">2000.00</font>
                </td>
              </tr>
              <tr>
                <td>
                  <font size="1">5</font>
                </td>
                <td>
                  <font size="1">Steve</font>
                </td>
                <td>
                  <font size="1">Smith</font>
                </td>
                <td>
                  <font size="1">900 Belle St</font>
                </td>
                <td>
                  <font size="1">Detroit</font>
                </td>
                <td>
                  <font size="1">MI</font>
                </td>
                <td>
                  <font size="1">48888</font>
                </td>
                <td>
                  <font size="1">5000.00</font>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <p>
Retrieving data is one of the most common tasks performed on a relational database. 
</p>
        <p>
Fortunately, SQL Server includes a language that allows users (and programs) to retrieve
the data they want. This language is called Structured Query Language. It is usually
abbreviated "SQL" and often pronounced "SEE-kwuhl". 
</p>
        <p>
Getting data from a database is known as querying the database. The code to retrieve
that data is a query. When this code is written in SQL (as most of my queries are),
it is known as a SQL query. 
</p>
        <p>
The basic syntax for a SQL query that retrieves data from a single table is 
</p>
        <p>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT
[List of Columns]<br />
    FROM [Table Name] 
<br />
    WHERE [Filter Condition] 
<br />
    ORDER BY [Sort column or columns] </span>
          </span>
        </p>
        <p>
In SQL, line breaks and extra spaces are not important. The language parser is smart
enough to figure out when a statement or command ends, so I usually try to format
my SQL statements to make them easy to read. 
</p>
        <p>
Only the <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT </span></span>and
FROM parts of the query are required, so let's start with those. 
</p>
        <p>
EvenEven though the "SELECT" keyword typically comes first in this type of query,
I'll start by explaining the "FROM" keyword. When getting data from only one table,
follow the keyword FROM with the name of that table. For example, the clause 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer </span>
        </pre>
        <p>
indicates that we are getting data from a table named Customer. We'll talk later about
how to get data from multiple tables in the same query. 
</p>
        <p>
It is possible to provide an alias for a table by following the table name with a
space, and the alias. For example 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer
cust</span>
        </pre>
        <p>
The above SQL clause says that we will get data from the Customer table, but that
we will use the string “cust” to refer to this table elsewhere in our query. 
</p>
        <p>
This is useful in the following situations 
</p>
        <ul>
          <li>
You want to provide a shorter name for the table in order to avoid retyping a long
name elsewhere in the query 
</li>
          <li>
You want to avoid ambiguity when listing the same table name twice in the FROM clause. 
</li>
        </ul>
        <p>
SELECT is the first keyword of this type of query and tells SQL Server that we want
to retrieve data from the database. The word "SELECT" is followed by a list of columns
that the query will return. If the column list contains duplicate column names (as
when you are getting data from two different tables and they each have a column with
the same name), you should precede the column name with the table name or alias. 
</p>
        <p>
You can also use the special character "*" in place of (or in addition to) the list
of column names in order to return all columns in the tables. 
</p>
        <p>
The following query returns all columns and rows in the Customer table. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span> *
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer </span>
        </pre>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font color="#000000" size="1">CustID </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font color="#000000" size="1">FirstName </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font color="#000000" size="1">LastName </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font color="#000000" size="1">StreetAddress </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font color="#000000" size="1">City </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font color="#000000" size="1">State </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font color="#000000" size="1">ZipCode </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font size="1">
                  <font color="#000000">TotalSales</font>
                </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">1</font>
              </td>
              <td>
                <font size="1">David</font>
              </td>
              <td>
                <font size="1">Giard</font>
              </td>
              <td>
                <font size="1">123 Oxford Ct</font>
              </td>
              <td>
                <font size="1">Erlanger</font>
              </td>
              <td>
                <font size="1">KY</font>
              </td>
              <td>
                <font size="1">40111</font>
              </td>
              <td>
                <font size="1">1000.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">2</font>
              </td>
              <td>
                <font size="1">Magic</font>
              </td>
              <td>
                <font size="1">Johnson</font>
              </td>
              <td>
                <font size="1">456 Hollywood Blvd</font>
              </td>
              <td>
                <font size="1">Lansing</font>
              </td>
              <td>
                <font size="1">MI</font>
              </td>
              <td>
                <font size="1">45222</font>
              </td>
              <td>
                <font size="1">1500.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">3</font>
              </td>
              <td>
                <font size="1">Bubba</font>
              </td>
              <td>
                <font size="1">Smith</font>
              </td>
              <td>
                <font size="1">789 Killbubbakill St</font>
              </td>
              <td>
                <font size="1">Baltimore</font>
              </td>
              <td>
                <font size="1">MD</font>
              </td>
              <td>
                <font size="1">10111</font>
              </td>
              <td>
                <font size="1">1000.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">4</font>
              </td>
              <td>
                <font size="1">Ron</font>
              </td>
              <td>
                <font size="1">Mason</font>
              </td>
              <td>
                <font size="1">501 E Grand River Ave</font>
              </td>
              <td>
                <font size="1">Okemos</font>
              </td>
              <td>
                <font size="1">MI</font>
              </td>
              <td>
                <font size="1">45333</font>
              </td>
              <td>
                <font size="1">2000.00</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">5</font>
              </td>
              <td>
                <font size="1">Steve</font>
              </td>
              <td>
                <font size="1">Smith</font>
              </td>
              <td>
                <font size="1">900 Belle St</font>
              </td>
              <td>
                <font size="1">Detroit</font>
              </td>
              <td>
                <font size="1">MI</font>
              </td>
              <td>
                <font size="1">48888</font>
              </td>
              <td>
                <font size="1">5000.00</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
We can return onWe can return only the FirstName and LastName columns from the Customer
table with the following table 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>         FirstName,
        LastName     <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer </span>
        </pre>
        <table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid">
          <tbody>
            <tr>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font size="1">FirstName </font>
              </td>
              <td style="BACKGROUND-COLOR: white; COLOR: black">
                <font size="1">LastName </font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">David</font>
              </td>
              <td>
                <font size="1">Giard</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">Magic</font>
              </td>
              <td>
                <font size="1">Johnson</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">Bubba</font>
              </td>
              <td>
                <font size="1">Smith</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">Ron</font>
              </td>
              <td>
                <font size="1">Mason</font>
              </td>
            </tr>
            <tr>
              <td>
                <font size="1">Steve</font>
              </td>
              <td>
                <font size="1">Smith</font>
              </td>
            </tr>
          </tbody>
        </table>
        <p>
In the queries above, it is possible to qualify the column names, explicitly indicating
that they are from the Customer table. Of course, in this case it is unnecessary because
the column names are unique within the Customer table. The following examples qualify
the column names and returns the same data. 
</p>
        <p>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>
            <br />
        Customer.FirstName, 
<br />
        Customer.LastName 
<br />
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer</span>
        </p>
        <p>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>
            <br />
        cu.FirstName, 
<br />
        cu.LastName 
<br />
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer
cu</span>
        </p>
        <p>
By default, each column returned by the query retains the name of the corresponding
column in the source table. you want to change the name of a If you want to change
the name of a column in the query, alias that column by appending the keyword " AS
" followed by the alias you want. For example 
</p>
        <p>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span>
            <br />
        FirstName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> FirstNm, 
<br />
        LastName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">AS</span> LastNm 
<br />
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer</span>
        </p>
This will return the following output 
<table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"><tbody><tr><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">FirstNm </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">LastNm </font></td></tr><tr><td><font size="1">David</font></td><td><font size="1">Giard</font></td></tr><tr><td><font size="1">Magic</font></td><td><font size="1">Johnson</font></td></tr><tr><td><font size="1">Bubba</font></td><td><font size="1">Smith</font></td></tr><tr><td><font size="1">Ron</font></td><td><font size="1">Mason</font></td></tr><tr><td><font size="1">Steve</font></td><td><font size="1">Smith</font></td></tr></tbody></table><p>
If wIf we want to sort the output we can add the ORDER BY clause to our query. The
syntax for this clause is 
</p><p><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">ORDER</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">BY</span></span>[List
of Columns on which to sort] 
</p><p>
For example, we can sort our output on Last Name by changing our query to 
</p><p><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span><br />
        FirstName, 
<br />
        LastName 
<br />
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer 
<br />
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">ORDER</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">BY</span> LastName </span></p><table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"><tbody><tr><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">FirstName </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">LastName </font></td></tr><tr><td><font size="1">David</font></td><td><font size="1">Giard</font></td></tr><tr><td><font size="1">Magic</font></td><td><font size="1">Johnson</font></td></tr><tr><td><font size="1">Steve</font></td><td><font size="1">Smith</font></td></tr><tr><td><font size="1">Bubba</font></td><td><font size="1">Smith</font></td></tr><tr><td><font size="1">Ron</font></td><td><font size="1">Mason</font></td></tr></tbody></table><p>
We can add more columns to the list of sort columns if we separate each with a comma.
The second column is only appropriate in our sort if two rows have identical values
for the first column. 
</p><p>
For example 
</p><p>
SELECT FirstNameFor example 
</p><p><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span><br />
        FirstName, 
<br />
        LastName 
<br />
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">FROM</span> Customer 
<br />
    <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">ORDER</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">BY</span> LastName,
FirstName</span></p><table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"><tbody><tr><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">FirstName </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">LastName </font></td></tr><tr><td><font size="1">David</font></td><td><font size="1">Giard</font></td></tr><tr><td><font size="1">Magic</font></td><td><font size="1">Johnson</font></td></tr><tr><td><font size="1">Bubba</font></td><td><font size="1">Smith</font></td></tr><tr><td><font size="1">Steve</font></td><td><font size="1">Smith</font></td></tr><tr><td><font size="1">Ron</font></td><td><font size="1">Mason</font></td></tr></tbody></table><p>
The above result set contains 2 rows with the last name "Smith".  These two rows
were sorted in order of their FirstName column.
</p><p>
In many cases, we may not want to return eIn many cases, we may not want to return
every row in a table. We can use the WHERE clause to filter data. The syntax of the
WHERE clause is 
</p><p><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">   WHERE</span></span>[Filter
Condition]
</p><p>
The Filter condition is a Boolean expression, meaning it evaluates to either TRUE
or FALSE for every row. The query will return only those rows for which this condition
evaluates to TRUE. 
</p><p>
For example, if we want to get only those customers in Michigan we can use the query 
</p><p><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT</span> * <br /><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">   FROM</span> Customer <br /><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">   WHERE</span> State
= <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">'MI'</span></span></p><table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"><tbody><tr><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">CustID</font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">FirstName </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">LastName </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">StreetAddress </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">City </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">State </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">ZipCode </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">TotalSales </font></td></tr><tr><td><font size="1">2</font></td><td><font size="1">Magic</font></td><td><font size="1">Johnson</font></td><td><font size="1">456 Hollywood Blvd</font></td><td><font size="1">Lansing</font></td><td><font size="1">MI</font></td><td><font size="1">45222</font></td><td><font size="1">1500.00</font></td></tr><tr><td><font size="1">4</font></td><td><font size="1">Ron</font></td><td><font size="1">Mason</font></td><td><font size="1">501 E Grand River Ave</font></td><td><font size="1">Okemos</font></td><td><font size="1">MI</font></td><td><font size="1">45333</font></td><td><font size="1">2000.00</font></td></tr><tr><td><font size="1">5</font></td><td><font size="1">Steve</font></td><td><font size="1">Smith</font></td><td><font size="1">900 Belle St</font></td><td><font size="1">Detroit</font></td><td><font size="1">MI</font></td><td><font size="1">48888</font></td><td><font size="1">5000.00</font></td></tr></tbody></table><p>
This query only returned those rows that match our filter condition.
</p><p>
Of course, we can combine several of these clauses as in the following
</p><p><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">SELECT 
<br />
        cu.FirstName, 
<br />
        cu.LastName,<br />
        cu.State AS ResidencyState<br />
    FROM Customer cu 
<br />
    WHERE cu.State = <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">'MI'</span><br />
    ORDER BY cu.LastName</span></span></p>
which returns the following results 
<table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"><tbody><tr><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">FirstName </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">LastName </font></td><td style="BACKGROUND-COLOR: white; COLOR: black"><font size="1">ResidencyState </font></td></tr><tr><td><font size="1">Magic</font></td><td><font size="1">Johnson</font></td><td><font size="1">MI</font></td></tr><tr><td><font size="1">Ron</font></td><td><font size="1">Mason</font></td><td><font size="1">MI</font></td></tr><tr><td><font size="1">Steve</font></td><td><font size="1">Smith</font></td><td><font size="1">MI</font></td></tr></tbody></table><p>
As you can see, we can use the SELECT commmand to retrieve data from a table in a
database and customize the way that data comes back.  We've just scratched the
surface of this command.  In the next article, we'll look at more options of
the SELECT command 
</p><img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=e319538e-1502-490c-870c-1a0bfc912bb7" /></body>
      <title>SQL 101 - Part 3: Selecting data from a table</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,e319538e-1502-490c-870c-1a0bfc912bb7.aspx</guid>
      <link>http://www.davidgiard.com/2009/03/10/SQL101Part3SelectingDataFromATable.aspx</link>
      <pubDate>Tue, 10 Mar 2009 16:36:54 GMT</pubDate>
      <description>&lt;img alt="Back To Basics" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;div style="BORDER-BOTTOM: thin solid; BORDER-LEFT: thin solid; BACKGROUND-COLOR: silver; MARGIN: 5px; WIDTH: 80%; BORDER-TOP: thin solid; BORDER-RIGHT: thin solid"&gt;
&lt;p&gt;
NOTE: 
&lt;/p&gt;
&lt;p&gt;
For demos in this article, we will use a table named Customer that contains&amp;nbsp;the
followong&amp;nbsp;columns: 
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
Name&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
Data Type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
FirstName&lt;/td&gt;
&lt;td&gt;
nvarchar(50)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
LastName&lt;/td&gt;
&lt;td&gt;
nvarchar(50)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
StreetAddress&lt;/td&gt;
&lt;td&gt;
nvarchar(255)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&amp;nbsp;City&lt;/td&gt;
&lt;td&gt;
nvarchar(255)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
State&lt;/td&gt;
&lt;td&gt;
char(2)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&amp;nbsp;ZipCode&lt;/td&gt;
&lt;td&gt;
nvarchar(10)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
TotalSales&lt;/td&gt;
&lt;td&gt;
money&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
Afer adding a few rows to the table, the data looks like this. 
&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;CustID &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;StreetAddress &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;State &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;TotalSales &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;123 Oxford Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Erlanger&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;KY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;40111&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;456 Hollywood Blvd&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Lansing&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;45222&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1500.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;789 Killbubbakill St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Baltimore&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MD&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;10111&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;501 E Grand River Ave&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Okemos&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;45333&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;2000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;
Retrieving data is one of the most common tasks performed on a relational database. 
&lt;/p&gt;
&lt;p&gt;
Fortunately, SQL Server includes a language that allows users (and programs) to retrieve
the data they want. This language is called Structured Query Language. It is usually
abbreviated "SQL" and often pronounced "SEE-kwuhl". 
&lt;/p&gt;
&lt;p&gt;
Getting data from a database is known as querying the database. The code to retrieve
that data is a query. When this code is written in SQL (as most of my queries are),
it is known as a SQL query. 
&lt;/p&gt;
&lt;p&gt;
The basic syntax for a SQL query that retrieves data from a single table is 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT
[List of Columns]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM [Table Name] 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE [Filter Condition] 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ORDER BY [Sort column or columns] &lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
In SQL, line breaks and extra spaces are not important. The language parser is smart
enough to figure out when a statement or command ends, so I usually try to format
my SQL statements to make them easy to read. 
&lt;/p&gt;
&lt;p&gt;
Only the &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT &lt;/span&gt;&lt;/span&gt;and
FROM parts of the query are required, so let's start with those. 
&lt;/p&gt;
&lt;p&gt;
EvenEven though the "SELECT" keyword typically comes first in this type of query,
I'll start by explaining the "FROM" keyword. When getting data from only one table,
follow the keyword FROM with the name of that table. For example, the clause 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
indicates that we are getting data from a table named Customer. We'll talk later about
how to get data from multiple tables in the same query. 
&lt;/p&gt;
&lt;p&gt;
It is possible to provide an alias for a table by following the table name with a
space, and the alias. For example 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer
cust&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The above SQL clause says that we will get data from the Customer table, but that
we will use the string “cust” to refer to this table elsewhere in our query. 
&lt;/p&gt;
&lt;p&gt;
This is useful in the following situations 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
You want to provide a shorter name for the table in order to avoid retyping a long
name elsewhere in the query 
&lt;li&gt;
You want to avoid ambiguity when listing the same table name twice in the FROM clause. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
SELECT is the first keyword of this type of query and tells SQL Server that we want
to retrieve data from the database. The word "SELECT" is followed by a list of columns
that the query will return. If the column list contains duplicate column names (as
when you are getting data from two different tables and they each have a column with
the same name), you should precede the column name with the table name or alias. 
&lt;/p&gt;
&lt;p&gt;
You can also use the special character "*" in place of (or in addition to) the list
of column names in order to return all columns in the tables. 
&lt;/p&gt;
&lt;p&gt;
The following query returns all columns and rows in the Customer table. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; *
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer &lt;/span&gt;&lt;/pre&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font color=#000000 size=1&gt;CustID &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font color=#000000 size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font color=#000000 size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font color=#000000 size=1&gt;StreetAddress &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font color=#000000 size=1&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font color=#000000 size=1&gt;State &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font color=#000000 size=1&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;&lt;font color=#000000&gt;TotalSales&lt;/font&gt; &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;1&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;123 Oxford Ct&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Erlanger&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;KY&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;40111&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;456 Hollywood Blvd&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Lansing&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;45222&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1500.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;3&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;789 Killbubbakill St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Baltimore&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MD&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;10111&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;501 E Grand River Ave&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Okemos&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;45333&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;2000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
We can return onWe can return only the FirstName and LastName columns from the Customer
table with the following table 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer &lt;/span&gt;&lt;/pre&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
In the queries above, it is possible to qualify the column names, explicitly indicating
that they are from the Customer table. Of course, in this case it is unnecessary because
the column names are unique within the Customer table. The following examples qualify
the column names and returns the same data. 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Customer.FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Customer.LastName 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cu.FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cu.LastName 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer
cu&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
By default, each column returned by the query retains the name of the corresponding
column in the source table. you want to change the name of a If you want to change
the name of a column in the query, alias that column by appending the keyword " AS
" followed by the alias you want. For example 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; FirstNm, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;AS&lt;/span&gt; LastNm 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer&lt;/span&gt;
&lt;/p&gt;
This will return the following output 
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;FirstNm &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;LastNm &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
If wIf we want to sort the output we can add the ORDER BY clause to our query. The
syntax for this clause is 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;ORDER&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;BY&lt;/span&gt; &lt;/span&gt;[List
of Columns on which to sort] 
&lt;/p&gt;
&lt;p&gt;
For example, we can sort our output on Last Name by changing our query to 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;ORDER&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;BY&lt;/span&gt; LastName &lt;/span&gt;
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
We can add more columns to the list of sort columns if we separate each with a comma.
The second column is only appropriate in our sort if two rows have identical values
for the first column. 
&lt;/p&gt;
&lt;p&gt;
For example 
&lt;/p&gt;
&lt;p&gt;
SELECT FirstNameFor example 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;FROM&lt;/span&gt; Customer 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;ORDER&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;BY&lt;/span&gt; LastName,
FirstName&lt;/span&gt;
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;David&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Giard&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Bubba&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
The above result set contains 2 rows with the last name "Smith".&amp;nbsp; These two rows
were sorted in order of their FirstName column.
&lt;/p&gt;
&lt;p&gt;
In many cases, we may not want to return eIn many cases, we may not want to return
every row in a table. We can use the WHERE clause to filter data. The syntax of the
WHERE clause is 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&lt;/span&gt; &lt;/span&gt;[Filter
Condition]
&lt;/p&gt;
&lt;p&gt;
The Filter condition is a Boolean expression, meaning it evaluates to either TRUE
or FALSE for every row. The query will return only those rows for which this condition
evaluates to TRUE. 
&lt;/p&gt;
&lt;p&gt;
For example, if we want to get only those customers in Michigan we can use the query 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT&lt;/span&gt; *&amp;nbsp;&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM&lt;/span&gt; Customer&amp;nbsp;&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&lt;/span&gt; State
= &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;'MI'&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;CustID&lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;StreetAddress &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;City &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;State &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;ZipCode &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;TotalSales &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;2&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;456 Hollywood Blvd&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Lansing&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;45222&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;1500.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;4&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;501 E Grand River Ave&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Okemos&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;45333&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;2000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;5&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;900 Belle St&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Detroit&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;48888&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;5000.00&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
This query only returned those rows that match our filter condition.
&lt;/p&gt;
&lt;p&gt;
Of course, we can combine several of these clauses as in the following
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;SELECT 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cu.FirstName, 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cu.LastName,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cu.State AS ResidencyState&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM Customer cu 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE cu.State = &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;'MI'&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ORDER BY cu.LastName&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
which returns the following results 
&lt;table style="BORDER-BOTTOM: white thin solid; TEXT-ALIGN: left; BORDER-LEFT: white thin solid; BACKGROUND-COLOR: gray; WIDTH: 57%; COLOR: white; BORDER-TOP: white thin solid; BORDER-RIGHT: white thin solid"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;FirstName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;LastName &lt;/font&gt;&lt;/td&gt;
&lt;td style="BACKGROUND-COLOR: white; COLOR: black"&gt;
&lt;font size=1&gt;ResidencyState &lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Magic&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Johnson&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Ron&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Mason&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;font size=1&gt;Steve&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;Smith&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font size=1&gt;MI&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
As you can see, we can use the SELECT commmand to retrieve data from a table in a
database and customize the way that data comes back.&amp;nbsp; We've just scratched the
surface of this command.&amp;nbsp; In the next article, we'll look at more options of
the SELECT command 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=e319538e-1502-490c-870c-1a0bfc912bb7" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,e319538e-1502-490c-870c-1a0bfc912bb7.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=eea3f49a-7347-468a-9375-97a4ee388c1a</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,eea3f49a-7347-468a-9375-97a4ee388c1a.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,eea3f49a-7347-468a-9375-97a4ee388c1a.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=eea3f49a-7347-468a-9375-97a4ee388c1a</wfw:commentRss>
      <slash:comments>21</slash:comments>
      <title>SQL 101 - Part 2: Relationships</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,eea3f49a-7347-468a-9375-97a4ee388c1a.aspx</guid>
      <link>http://www.davidgiard.com/2009/02/26/SQL101Part2Relationships.aspx</link>
      <pubDate>Thu, 26 Feb 2009 13:17:33 GMT</pubDate>
      <description>&lt;img style="FLOAT: right; MARGIN-RIGHT: 5px" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;p&gt;
In &lt;a href="http://www.davidgiard.com/2009/02/24/SQL101Part1WhatIsADatabase.aspx"&gt;my
last article&lt;/a&gt;, I defined the basic concepts of database, table, column and row.
Using these constructs, you can organize data into a rectangular format. This paradigm
often works really well, because 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
You can group related information into a single container (a table) 
&lt;li&gt;
Each row represents a single entity (such as a customer, employee, or invoice) and 
&lt;li&gt;
Each column represents an attribute of the entity (such as FirstName, LastName, or
TotalSales).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Using this model, we can create a table containing information about a customer's
purchases. Each row in this item might represent a single purchase. 
&lt;/p&gt;
&lt;p&gt;
When a customer purchases an item, we probably would want to store some information
about that purchase. These bits of information about each purchase are attributes
of the purchase and are therefore candidates for columns. Below are examples of the
information we might want to save about a customer's purchase. 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Date of Purchase 
&lt;li&gt;
Customer First Name 
&lt;li&gt;
Customer Last Name 
&lt;li&gt;
Customer Street Address 
&lt;li&gt;
Customer City 
&lt;li&gt;
Customer Zip Code 
&lt;li&gt;
Item Purchased 
&lt;li&gt;
Quantity Purchased 
&lt;li&gt;
Price per Item 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
We can create a table &lt;i&gt;CustomerPurchase&lt;/i&gt; with a column for each of the above
attributes and begin populating with data each time a customer purchases something.
The data would look something like this: 
&lt;/p&gt;
&lt;table border=1 cellpadding=0 cellspacing: 0&gt;
&lt;tbody&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
PurchaseDate&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
FirstName&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
LastName&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
StreetAddress&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
City&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
ZipCode&lt;/td&gt;
&lt;td&gt;
ItemPurchased&lt;/td&gt;
&lt;td&gt;
Quantity&lt;/td&gt;
&lt;td&gt;
PricePerItem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
John&lt;/td&gt;
&lt;td&gt;
Smith&lt;/td&gt;
&lt;td&gt;
123 Elm&lt;/td&gt;
&lt;td&gt;
Bigg City&lt;/td&gt;
&lt;td&gt;
48222&lt;/td&gt;
&lt;td&gt;
Lamp&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
40&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
Bill&lt;/td&gt;
&lt;td&gt;
Jones&lt;/td&gt;
&lt;td&gt;
456 Maple&lt;/td&gt;
&lt;td&gt;
Smallville&lt;/td&gt;
&lt;td&gt;
48333&lt;/td&gt;
&lt;td&gt;
Chair&lt;/td&gt;
&lt;td&gt;
2&lt;/td&gt;
&lt;td&gt;
100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
Mary&lt;/td&gt;
&lt;td&gt;
Brown&lt;/td&gt;
&lt;td&gt;
789 Oak&lt;/td&gt;
&lt;td&gt;
Middleton&lt;/td&gt;
&lt;td&gt;
48444&lt;/td&gt;
&lt;td&gt;
Table&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
50&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
This model seems to capture the information we want. Do you see any problems with
it? 
&lt;/p&gt;
&lt;p&gt;
What happens if a customer orders more than one item? If John Smith purchases a Chair
in addition to his Lamp, we can just add another row to the table, like so. 
&lt;/p&gt;
&lt;table border=1 cellspacing=0 cellpadding=0&gt;
&lt;tbody&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
PurchaseDate&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
FirstName&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
LastName&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
StreetAddress&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
City&lt;/td&gt;
&lt;td&gt;
Customer&lt;br&gt;
ZipCode&lt;/td&gt;
&lt;td&gt;
ItemPurchased&lt;/td&gt;
&lt;td&gt;
Quantity&lt;/td&gt;
&lt;td&gt;
PricePerItem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
John&lt;/td&gt;
&lt;td&gt;
Smith&lt;/td&gt;
&lt;td&gt;
123 Elm&lt;/td&gt;
&lt;td&gt;
Bigg City&lt;/td&gt;
&lt;td&gt;
48222&lt;/td&gt;
&lt;td&gt;
Lamp&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
40&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
Bill&lt;/td&gt;
&lt;td&gt;
Jones&lt;/td&gt;
&lt;td&gt;
456 Maple&lt;/td&gt;
&lt;td&gt;
Smallville&lt;/td&gt;
&lt;td&gt;
48333&lt;/td&gt;
&lt;td&gt;
Chair&lt;/td&gt;
&lt;td&gt;
2&lt;/td&gt;
&lt;td&gt;
100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
Mary&lt;/td&gt;
&lt;td&gt;
Brown&lt;/td&gt;
&lt;td&gt;
789 Oak&lt;/td&gt;
&lt;td&gt;
Middleton&lt;/td&gt;
&lt;td&gt;
48444&lt;/td&gt;
&lt;td&gt;
Table&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
John&lt;/td&gt;
&lt;td&gt;
Smith&lt;/td&gt;
&lt;td&gt;
123 Elm&lt;/td&gt;
&lt;td&gt;
Bigg City&lt;/td&gt;
&lt;td&gt;
48222&lt;/td&gt;
&lt;td&gt;
Chair&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td height=20&gt;
2/27/2009&lt;/td&gt;
&lt;td&gt;
John&lt;/td&gt;
&lt;td&gt;
Smith&lt;/td&gt;
&lt;td&gt;
123 Elm&lt;/td&gt;
&lt;td&gt;
Bigg City&lt;/td&gt;
&lt;td&gt;
48222&lt;/td&gt;
&lt;td&gt;
Table&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
50&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
But notice that now we are storing John Smith's name and address multiple times.&amp;nbsp;
Assuming John Smith will never change his name, this is a waste of space.&amp;nbsp; Granted,
this isn't very much wasted space when we have only a few orders, but imagine a system
with thousands of customers and millions of orders.&amp;nbsp; Do you really want all that
redundant information cluttering up your database? 
&lt;/p&gt;
&lt;p&gt;
Also, imagine that we want to correct an error in the spelling of John's name.&amp;nbsp;
With the current model, we must correct that error three times due to the redundant
storage.
&lt;/p&gt;
&lt;p&gt;
To address these issues, we can normalize the data.&amp;nbsp; Data normalization refers
to structuring our data in order to remove redundancy.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
In our example, we accomplish this by creating a table of customers with the following
structure 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
FirstName 
&lt;li&gt;
LastName 
&lt;li&gt;
StreetAddress 
&lt;li&gt;
City 
&lt;li&gt;
ZipCode 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
and and moving the customer data to this table - one row per customer. 
&lt;/p&gt;
&lt;p&gt;
&lt;table border=1 cellspacing=0 cellpadding=0&gt;
&lt;tbody&gt;
&lt;tr height=20&gt;
&lt;td&gt;
FirstName&lt;/td&gt;
&lt;td&gt;
LastName&lt;/td&gt;
&lt;td&gt;
StreetAddress&lt;/td&gt;
&lt;td&gt;
City&lt;/td&gt;
&lt;td&gt;
ZipCode&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td&gt;
John&lt;/td&gt;
&lt;td&gt;
Smith&lt;/td&gt;
&lt;td&gt;
123 Elm&lt;/td&gt;
&lt;td&gt;
Bigg City&lt;/td&gt;
&lt;td&gt;
48222&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td&gt;
Bill&lt;/td&gt;
&lt;td&gt;
Jones&lt;/td&gt;
&lt;td&gt;
456 Maple&lt;/td&gt;
&lt;td&gt;
Smallville&lt;/td&gt;
&lt;td&gt;
48333&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td&gt;
Mary&lt;/td&gt;
&lt;td&gt;
Brown&lt;/td&gt;
&lt;td&gt;
789 Oak&lt;/td&gt;
&lt;td&gt;
Middleton&lt;/td&gt;
&lt;td&gt;
48444&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Then we add an extra column to the ustomerPurchase&gt; tab table.&amp;nbsp; This new column
is special in that the value in it will uniquely identify each row - in other words,
no two rows will have the same value.&amp;nbsp; This unique column goes by many names
but we will call it a Primary Key here.&amp;nbsp; In this case, the Primary Key column
will be named "CustomerID" and will hold an integer. 
&lt;/p&gt;
&lt;p&gt;
&lt;table border=1 cellspacing=0 cellpadding=0&gt;
&lt;tbody&gt;
&lt;tr height=20&gt;
&lt;td&gt;
CustomerID&lt;/td&gt;
&lt;td&gt;
FirstName&lt;/td&gt;
&lt;td&gt;
LastName&lt;/td&gt;
&lt;td&gt;
StreetAddress&lt;/td&gt;
&lt;td&gt;
City&lt;/td&gt;
&lt;td&gt;
ZipCode&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
John&lt;/td&gt;
&lt;td&gt;
Smith&lt;/td&gt;
&lt;td&gt;
123 Elm&lt;/td&gt;
&lt;td&gt;
Bigg City&lt;/td&gt;
&lt;td&gt;
48222&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td&gt;
2&lt;/td&gt;
&lt;td&gt;
Bill&lt;/td&gt;
&lt;td&gt;
Jones&lt;/td&gt;
&lt;td&gt;
456 Maple&lt;/td&gt;
&lt;td&gt;
Smallville&lt;/td&gt;
&lt;td&gt;
48333&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td&gt;
3&lt;/td&gt;
&lt;td&gt;
Mary&lt;/td&gt;
&lt;td&gt;
Brown&lt;/td&gt;
&lt;td&gt;
789 Oak&lt;/td&gt;
&lt;td&gt;
Middleton&lt;/td&gt;
&lt;td&gt;
48444&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Now we can go back to the ustomerPurchase&gt; tab table, and replace the columns that
describe customer with a column to hold the CustomerID.&amp;nbsp; This&amp;nbsp;replacement
column&amp;nbsp;is known as a "Foreign Key".&amp;nbsp; It references a Primary Key in another
table and is used to point to a single unique record in that other table.
&lt;/p&gt;
&lt;p&gt;
&lt;table border=1 cellspacing=0 cellpadding=0&gt;
&lt;tbody&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
PurchaseDate&lt;/td&gt;
&lt;td&gt;
CustomerID&lt;/td&gt;
&lt;td&gt;
ItemPurchased&lt;/td&gt;
&lt;td&gt;
Quantity&lt;/td&gt;
&lt;td&gt;
PricePerItem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
Lamp&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
40&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
2&lt;/td&gt;
&lt;td&gt;
Chair&lt;/td&gt;
&lt;td&gt;
2&lt;/td&gt;
&lt;td&gt;
100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr height=20&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
3&lt;/td&gt;
&lt;td&gt;
Table&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td height=20&gt;
2/26/2009&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
Chair&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td height=20&gt;
2/27/2009&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
Table&lt;/td&gt;
&lt;td&gt;
1&lt;/td&gt;
&lt;td&gt;
50&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
This is all we need because, given the CustomerID, we can look in the &lt;em&gt;Customer&lt;/em&gt; table,
find the record for that customer and get all information about that customer. 
&lt;p&gt;
This concept of using a key value to point to a row in another table is known as a
relationship.&amp;nbsp; We say that the &lt;i&gt;Customer&lt;/i&gt; table is related to the &lt;i&gt;CustomerPurchase&lt;/i&gt; tab
table.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
This type of relationship is known as a one-to-many relationship, every customer may
have many orders.&amp;nbsp; In this type of relationship the table with one row is known
as the parent and the table with (potentially) many rows is known as the child table.&amp;nbsp;&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
This relationship is typically represented by a drawing similar to the one below.
&lt;/p&gt;
&lt;p&gt;
&lt;img border=0 src="http://www.davidgiard.com/content/binary/DbRelationship.jpg"&gt;
&lt;/p&gt;
&lt;p&gt;
Organizing data in this way can make storage of that data far more efficient and flexible.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=eea3f49a-7347-468a-9375-97a4ee388c1a" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,eea3f49a-7347-468a-9375-97a4ee388c1a.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=f07efbfb-04c9-4db1-bd79-9300f5eaeee2</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,f07efbfb-04c9-4db1-bd79-9300f5eaeee2.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,f07efbfb-04c9-4db1-bd79-9300f5eaeee2.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f07efbfb-04c9-4db1-bd79-9300f5eaeee2</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img style="FLOAT: right; MARGIN-RIGHT: 5px" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        <p>
In this article, we will define a database, a table and the main parts of a table
- rows and columns.
</p>
        <p>
A database is an organized (or structured) collection of information. 
</p>
        <p>
Most companies spend a lot of time and effort collecting information and storing it
somewhere, but not all that information is organized, which makes it difficult to
retrieve anything relevant later on.  A database adds structure to the information
making it easier to maintain and query it.
</p>
        <p>
Database engines like SQL Server provide a structure to organize data in a way that
makes sense to a user.  Specifically, SQL Server uses a relational model* to
organize its data. 
</p>
        <p>
In a relational database, data is partitioned into tables.  Tables are a way
of data storing data in rows and columns, kind of like in an Excel worksheet.  
</p>
        <p>
          <img border="0" src="http://www.davidgiard.com/content/binary/ExcelSample.bmp" />
          <br />
          <strong>
            <font size="1">Figure 1 - Data in an Excel workbook</font>
          </strong>
        </p>
        <p>
I've always found this rectangular view of data very intuitive.
</p>
        <p>
Just as in a workbook, each table row represents a discrete record.  All information
in that row serves to describe the row.   
</p>
        <p>
Similarly, a table column is a placeholder that describes a single attribute for each
row.  The advantage SQL Server has over Excel is that you can easily place rules
onto a column, restricting the type of data that can be stored there.  
</p>
        <p>
If a SQL Server column is designed to hold a date, a property of that column can be
set to throw an error if a person or program tries to store a string.  
We can set up such restrictions for many data types, so that a column can be restricted
to allow only integers, only TRUE/FALSE values, or only binary objects.  We can
even restrict the maximum length of a string or require users to always enter a value
into a column - all simply by setting properties on the column.**
</p>
        <p>
For example, a table named "Customers" might be used to store information about your
company's customers.  Each row in this table would represent a single customer. 
Each column would hold an attribute of that customer, so you could create columns
such as FirstName, LastName and StreetAddress that would hold the appropriate values
for each customer.  
</p>
        <p>
          <img border="0" src="http://www.davidgiard.com/content/binary/SqlServerTableSample.bmp" />
          <br />
          <strong>
            <font size="1">Figure 2 - Data in a SQL Server table</font>
          </strong>
        </p>
        <p>
Looking at the first row, gives us information about the customer.  It should
be obvious that this customer has a first name of "David", a last name of "Giard"
and an address of "123 Main St".
</p>
        <hr />
        <p>
*SQL Server does provide some non-relational ways of storing data but those are beyond
the scope of this article.<br />
** It is possible to configure Microsoft Excel to restrict data input, but this task
is relatively advanced and far more easily accomplished in SQL Server.
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=f07efbfb-04c9-4db1-bd79-9300f5eaeee2" />
      </body>
      <title>SQL 101 - Part 1: What is a Database?</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,f07efbfb-04c9-4db1-bd79-9300f5eaeee2.aspx</guid>
      <link>http://www.davidgiard.com/2009/02/24/SQL101Part1WhatIsADatabase.aspx</link>
      <pubDate>Tue, 24 Feb 2009 18:14:48 GMT</pubDate>
      <description>&lt;img style="FLOAT: right; MARGIN-RIGHT: 5px" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;p&gt;
In this article, we will define a database, a table and the&amp;nbsp;main parts of a table
- rows and columns.
&lt;/p&gt;
&lt;p&gt;
A database is an organized (or structured) collection of information. 
&lt;/p&gt;
&lt;p&gt;
Most companies spend a lot of time and effort collecting information and storing it
somewhere, but not all that information is organized, which makes it difficult to
retrieve anything relevant later on.&amp;nbsp; A database adds structure to the information
making it easier to maintain and query it.
&lt;/p&gt;
&lt;p&gt;
Database engines like SQL Server provide a structure to organize data in a way that
makes sense to a user.&amp;nbsp; Specifically, SQL Server uses a relational model* to
organize its data. 
&lt;/p&gt;
&lt;p&gt;
In a relational database, data is partitioned into tables.&amp;nbsp; Tables are a way
of data storing data in rows and columns, kind of like in an Excel worksheet.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
&lt;img border=0 src="http://www.davidgiard.com/content/binary/ExcelSample.bmp"&gt;
&lt;br&gt;
&lt;strong&gt;&lt;font size=1&gt;Figure 1 - Data in an Excel workbook&lt;/font&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I've always found this rectangular view of data very intuitive.
&lt;/p&gt;
&lt;p&gt;
Just as in a workbook, each table row represents a discrete record.&amp;nbsp; All information
in that row serves to describe the row.&amp;nbsp;&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Similarly, a table column is a placeholder that describes a single attribute for each
row.&amp;nbsp; The advantage SQL Server has over Excel is that you can easily place rules
onto a column, restricting the type of data that can be stored there.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
If a SQL Server column is designed to hold a date, a property of that column can be
set to throw an error if a person or program tries to store a string.&amp;nbsp;&amp;nbsp;
We can set up such restrictions for many data types, so that a column can be restricted
to allow only integers, only TRUE/FALSE values, or only binary objects.&amp;nbsp; We can
even restrict the maximum length of a string or require users to always enter a value
into a column - all simply by setting properties on the column.**
&lt;/p&gt;
&lt;p&gt;
For example, a table named "Customers" might be used to store information about your
company's customers.&amp;nbsp; Each row in this table would represent a single customer.&amp;nbsp;
Each column would hold an attribute of that customer, so you could create columns
such as FirstName, LastName and StreetAddress that would hold the appropriate values
for each customer.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
&lt;img border=0 src="http://www.davidgiard.com/content/binary/SqlServerTableSample.bmp"&gt;
&lt;br&gt;
&lt;strong&gt;&lt;font size=1&gt;Figure&amp;nbsp;2 - Data in a SQL Server table&lt;/font&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Looking at the first row, gives us information about the customer.&amp;nbsp; It should
be obvious that this customer has a first name of "David", a last name of "Giard"
and an address of "123 Main St".
&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;
*SQL Server does provide some non-relational ways of storing data but those are beyond
the scope of this article.&lt;br&gt;
** It is possible to configure Microsoft Excel to restrict data input, but this task
is relatively advanced and far more easily accomplished in SQL Server.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=f07efbfb-04c9-4db1-bd79-9300f5eaeee2" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,f07efbfb-04c9-4db1-bd79-9300f5eaeee2.aspx</comments>
      <category>Back To Basics</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://www.davidgiard.com/Trackback.aspx?guid=c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966</trackback:ping>
      <pingback:server>http://www.davidgiard.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.davidgiard.com/PermaLink,guid,c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966.aspx</pingback:target>
      <dc:creator>David Giard</dc:creator>
      <wfw:comment>http://www.davidgiard.com/CommentView,guid,c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966.aspx</wfw:comment>
      <wfw:commentRss>http://www.davidgiard.com/SyndicationService.asmx/GetEntryCommentsRss?guid=c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <img style="FLOAT: right; MARGIN-RIGHT: 5px" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG" />
        <p>
I deal with a lot of smart, passionate people with years of experience in technology.  
</p>
        <p>
I've noticed that these folks love to talk about and write about the next generation
of software and advanced topics in software development.  This is great because
it gives me a chance to learn new things from the smart people in my life.  
</p>
        <p>
Unfortunately, not everyone is ready for advanced topics.  Developers who are
just starting their careers need to understand the basics of languages, programming
constructs and relational databases before diving deeper into these and other areas. 
These basic topics are often less interesting to experienced developers but they are
vitally important.  And who is better capable of explaining them than an experienced
developer or architect?
</p>
        <p>
I'll address this partial vacuum in a new <em>Back To Basics </em>feature on
this site.  In this feature, I'll explain some fundamental concepts of software
development, assuming little or no experience on the part of the reader.
</p>
        <p>
The feature begins this week with a set of articles entitled SQL Server 101,
in which I'll describe the basics of SQL Server and relational databases.  This
will tie in with a talk I'll be giving at the <a href="http://dodn.org/WestMichiganDotNetU/" target="_blank">West
Michigan .Net University</a> April 4 in Grand Rapids, MI.
</p>
        <img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966" />
      </body>
      <title>Back To Basics</title>
      <guid isPermaLink="false">http://www.davidgiard.com/PermaLink,guid,c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966.aspx</guid>
      <link>http://www.davidgiard.com/2009/02/22/BackToBasics.aspx</link>
      <pubDate>Sun, 22 Feb 2009 04:42:15 GMT</pubDate>
      <description>&lt;img style="FLOAT: right; MARGIN-RIGHT: 5px" src="http://www.davidgiard.com/content/binary/BackToBasics.JPG"&gt; 
&lt;p&gt;
I deal with a lot of smart, passionate people with years of experience in technology.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
I've noticed that these folks love to talk about and write about the next generation
of software and advanced topics in software development.&amp;nbsp; This is great because
it gives me a chance to learn new things from the smart people in my life.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Unfortunately, not everyone is ready for advanced topics.&amp;nbsp; Developers who are
just starting their careers need to understand the basics of languages, programming
constructs and relational databases before diving deeper into these and other areas.&amp;nbsp;
These basic topics are often less interesting to experienced developers but they are
vitally important.&amp;nbsp; And who is better capable of explaining them than an experienced
developer or architect?
&lt;/p&gt;
&lt;p&gt;
I'll address this partial vacuum in a new &lt;em&gt;Back To Basics&amp;nbsp;&lt;/em&gt;feature on
this site.&amp;nbsp; In this feature, I'll explain some fundamental concepts of software
development, assuming little or no experience on the part of the reader.
&lt;/p&gt;
&lt;p&gt;
The feature begins this week with a set of articles&amp;nbsp;entitled SQL Server 101,
in which I'll describe the basics of SQL Server and relational databases.&amp;nbsp; This
will tie in with a talk I'll be giving at the &lt;a href="http://dodn.org/WestMichiganDotNetU/" target=_blank&gt;West
Michigan .Net University&lt;/a&gt; April 4 in Grand Rapids, MI.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.davidgiard.com/aggbug.ashx?id=c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966" /&gt;</description>
      <comments>http://www.davidgiard.com/CommentView,guid,c0e14e7a-ca1d-44f2-85e1-a4f04ed1d966.aspx</comments>
      <category>Back To Basics</category>
    </item>
  </channel>
</rss>