<?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:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Mo Khan: My Blog! - ADO.NET</title>
    <link>http://mokhan.ca/blog/</link>
    <description>Update your gray matter, because one day it may matter!</description>
    <language>en-us</language>
    <copyright>Mo Khan</copyright>
    <lastBuildDate>Wed, 11 Jun 2008 04:19:17 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>mo@mokhan.ca</managingEditor>
    <webMaster>mo@mokhan.ca</webMaster>
    <item>
      <trackback:ping>http://mokhan.ca/blog/Trackback.aspx?guid=3612b693-b263-41da-98f2-3717bb790166</trackback:ping>
      <pingback:server>http://mokhan.ca/blog/pingback.aspx</pingback:server>
      <pingback:target>http://mokhan.ca/blog/PermaLink,guid,3612b693-b263-41da-98f2-3717bb790166.aspx</pingback:target>
      <dc:creator>Mr mO!</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
An idea the team an I had today, was to build a more fluent interface for creating
dynamic SQL queries. Here's what I mean:
</p>
        <pre class="code">[<span style="color: #2b91af">TestFixture</span>] <span style="color: blue">public
class </span><span style="color: #2b91af">when_creating_an_insert_query_for_two_or_more_columns </span>{
[<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>should_return_the_correct_sql()
{ <span style="color: blue">var </span>query = <span style="color: #2b91af">Insert</span>.Into&lt;<span style="color: #2b91af">CustomersTable</span>&gt;()
.ValueOf(<span style="color: #a31515">"mo"</span>).ForColumn(c =&gt; c.FirstName())
.And() .ValueOf(<span style="color: #a31515">"khan"</span>).ForColumn(c
=&gt; c.LastName()) .End(); <span style="color: blue">var </span>expected = <span style="color: #a31515">"INSERT
INTO Customers ( FirstName, LastName ) VALUES ( @FirstName, @LastName );"</span>;
query.ToSql().ShouldBeEqualTo(expected); } }</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>It's the responsibility of the query
object to prepare the command with the command parameter names and values, so in this
test I'm just focused on the raw sql. One of the benefits of this API, is that it's
strongly typed, so you can't stick a string in a column represented by a long. 
</p>
        <p>
For example, Imagine a customers table that looks like this:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">CustomersTable </span>: <span style="color: #2b91af">IDatabaseTable </span>{ <span style="color: blue">public
string </span>Name() { <span style="color: blue">return </span><span style="color: #a31515">"Customers"</span>;
} <span style="color: blue">public </span><span style="color: #2b91af">IDatabaseColumn</span>&lt;<span style="color: blue">long</span>&gt;
Id() { <span style="color: blue">return new </span><span style="color: #2b91af">DatabaseColumn</span>&lt;<span style="color: blue">long</span>&gt;(<span style="color: #a31515">"Id"</span>);
} <span style="color: blue">public </span><span style="color: #2b91af">IDatabaseColumn</span>&lt;<span style="color: blue">string</span>&gt;
FirstName() { <span style="color: blue">return new </span><span style="color: #2b91af">DatabaseColumn</span>&lt;<span style="color: blue">string</span>&gt;(<span style="color: #a31515">"FirstName"</span>);
} <span style="color: blue">public </span><span style="color: #2b91af">IDatabaseColumn</span>&lt;<span style="color: blue">string</span>&gt;
LastName() { <span style="color: blue">return new </span><span style="color: #2b91af">DatabaseColumn</span>&lt;<span style="color: blue">string</span>&gt;(<span style="color: #a31515">"LastName"</span>);
} }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Here's what we've got so far for contracts... 
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">Insert </span>{ <span style="color: blue">public
static </span><span style="color: #2b91af">ITableSelector</span>&lt;Table&gt; Into&lt;Table&gt;() <span style="color: blue">where </span>Table
: <span style="color: #2b91af">IDatabaseTable </span>{ <span style="color: blue">return
new </span><span style="color: #2b91af">TableSelector</span>&lt;Table&gt;(); } } <span style="color: blue">public
interface </span><span style="color: #2b91af">ITableSelector</span>&lt;Table&gt; { <span style="color: #2b91af">IColumnSelector</span>&lt;Table,
ColumnType&gt; ValueOf&lt;ColumnType&gt;(ColumnType value); } <span style="color: blue">public
interface </span><span style="color: #2b91af">IColumnSelector</span>&lt;Table, ColumnType&gt;
{ <span style="color: #2b91af">IChainedSelector</span>&lt;Table&gt; ForColumn&lt;TColumn&gt;(<span style="color: #2b91af">Func</span>&lt;Table,
TColumn&gt; columnSelection) <span style="color: blue">where </span>TColumn : <span style="color: #2b91af">IDatabaseColumn</span>&lt;ColumnType&gt;;
} <span style="color: blue">public interface </span><span style="color: #2b91af">IChainedSelector</span>&lt;Table&gt;
{ <span style="color: #2b91af">ITableSelector</span>&lt;Table&gt; And(); <span style="color: #2b91af">IQuery </span>End();
}</pre>
        <p>
And here's as far as we got with the implementation...
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">TableSelector</span>&lt;Table&gt;
: <span style="color: #2b91af">ITableSelector</span>&lt;Table&gt; <span style="color: blue">where </span>Table
: <span style="color: #2b91af">IDatabaseTable </span>{ <span style="color: blue">public </span><span style="color: #2b91af">IColumnSelector</span>&lt;Table,
T&gt; ValueOf&lt;T&gt;(T value) { <span style="color: blue">return new </span><span style="color: #2b91af">ColumnSelector</span>&lt;Table,
T&gt;(value); } } <span style="color: blue">public class </span><span style="color: #2b91af">ColumnSelector</span>&lt;Table,
T&gt; : <span style="color: #2b91af">IColumnSelector</span>&lt;Table, T&gt; <span style="color: blue">where </span>Table
: <span style="color: #2b91af">IDatabaseTable </span>{ <span style="color: blue">private
readonly </span>T value; <span style="color: blue">public </span>ColumnSelector(T
value) { <span style="color: blue">this</span>.value = value; } <span style="color: blue">public </span><span style="color: #2b91af">IChainedSelector</span>&lt;Table&gt;
ForColumn&lt;TColumn&gt;(<span style="color: #2b91af">Func</span>&lt;Table, TColumn&gt;
columnSelection) <span style="color: blue">where </span>TColumn : <span style="color: #2b91af">IDatabaseColumn</span>&lt;T&gt;
{ <span style="color: blue">var </span>table = <span style="color: #2b91af">Activator</span>.CreateInstance&lt;Table&gt;(); <span style="color: blue">return
new </span><span style="color: #2b91af">ChainedSelector</span>&lt;Table, T, TColumn&gt;(
table, value, columnSelection(table) ); } } <span style="color: blue">public class </span><span style="color: #2b91af">ChainedSelector</span>&lt;Table,
Value, Column&gt; : <span style="color: #2b91af">IChainedSelector</span>&lt;Table&gt; <span style="color: blue">where </span>Table
: <span style="color: #2b91af">IDatabaseTable </span><span style="color: blue">where </span>Column
: <span style="color: #2b91af">IDatabaseColumn</span>&lt;Value&gt; { <span style="color: blue">private
readonly </span>Table table; <span style="color: blue">private readonly </span>Value
value; <span style="color: blue">private readonly </span>Column column; <span style="color: blue">public </span>ChainedSelector(Table
table, Value value, Column column) { <span style="color: blue">this</span>.table =
table; <span style="color: blue">this</span>.value = value; <span style="color: blue">this</span>.column
= column; } <span style="color: blue">public </span><span style="color: #2b91af">ITableSelector</span>&lt;Table&gt;
And() { <span style="color: blue">throw new </span><span style="color: #2b91af">NotImplementedException</span>();
} <span style="color: blue">public </span><span style="color: #2b91af">IQuery </span>End()
{ <span style="color: blue">var </span>builder = <span style="color: blue">new </span><span style="color: #2b91af">InsertStatementBuilder</span>(table.Name());
builder.Add(column, value); <span style="color: blue">return </span>builder.EndQuery();
} }</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>The most important piece is still
missing, and that's implementing the "And()" method on ChainedSelector...
and finishing off the End method. I'm drawing a blank.. Thoughts are appreciated!
</p>
        <p>
 
</p>
        <p>
          <a href="http://11011.net/software/vspaste"> </a>
        </p>
        <img width="0" height="0" src="http://mokhan.ca/blog/aggbug.ashx?id=3612b693-b263-41da-98f2-3717bb790166" />
      </body>
      <title>A Strongly Typed SQL Query API... almost!</title>
      <guid isPermaLink="false">http://mokhan.ca/blog/PermaLink,guid,3612b693-b263-41da-98f2-3717bb790166.aspx</guid>
      <link>http://mokhan.ca/blog/2008/06/11/A+Strongly+Typed+SQL+Query+API+Almost.aspx</link>
      <pubDate>Wed, 11 Jun 2008 04:19:17 GMT</pubDate>
      <description>&lt;p&gt;
An idea the team an I had today, was to build a more fluent interface for creating
dynamic SQL queries. Here's what I mean:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestFixture&lt;/span&gt;] &lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;when_creating_an_insert_query_for_two_or_more_columns &lt;/span&gt;{
[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public void &lt;/span&gt;should_return_the_correct_sql()
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;query = &lt;span style="color: #2b91af"&gt;Insert&lt;/span&gt;.Into&amp;lt;&lt;span style="color: #2b91af"&gt;CustomersTable&lt;/span&gt;&amp;gt;()
.ValueOf(&lt;span style="color: #a31515"&gt;&amp;quot;mo&amp;quot;&lt;/span&gt;).ForColumn(c =&amp;gt; c.FirstName())
.And() .ValueOf(&lt;span style="color: #a31515"&gt;&amp;quot;khan&amp;quot;&lt;/span&gt;).ForColumn(c
=&amp;gt; c.LastName()) .End(); &lt;span style="color: blue"&gt;var &lt;/span&gt;expected = &lt;span style="color: #a31515"&gt;&amp;quot;INSERT
INTO Customers ( FirstName, LastName ) VALUES ( @FirstName, @LastName );&amp;quot;&lt;/span&gt;;
query.ToSql().ShouldBeEqualTo(expected); } }&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;It's the responsibility of the query
object to prepare the command with the command parameter names and values, so in this
test I'm just focused on the raw sql. One of the benefits of this API, is that it's
strongly typed, so you can't stick a string in a column represented by a long. 
&lt;/p&gt;
&lt;p&gt;
For example, Imagine a customers table that looks like this:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomersTable &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IDatabaseTable &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
string &lt;/span&gt;Name() { &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Customers&amp;quot;&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDatabaseColumn&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;long&lt;/span&gt;&amp;gt;
Id() { &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DatabaseColumn&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;long&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;);
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDatabaseColumn&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
FirstName() { &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DatabaseColumn&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;);
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDatabaseColumn&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
LastName() { &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DatabaseColumn&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt;);
} }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Here's what we've got so far for contracts... 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Insert &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ITableSelector&lt;/span&gt;&amp;lt;Table&amp;gt; Into&amp;lt;Table&amp;gt;() &lt;span style="color: blue"&gt;where &lt;/span&gt;Table
: &lt;span style="color: #2b91af"&gt;IDatabaseTable &lt;/span&gt;{ &lt;span style="color: blue"&gt;return
new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TableSelector&lt;/span&gt;&amp;lt;Table&amp;gt;(); } } &lt;span style="color: blue"&gt;public
interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ITableSelector&lt;/span&gt;&amp;lt;Table&amp;gt; { &lt;span style="color: #2b91af"&gt;IColumnSelector&lt;/span&gt;&amp;lt;Table,
ColumnType&amp;gt; ValueOf&amp;lt;ColumnType&amp;gt;(ColumnType value); } &lt;span style="color: blue"&gt;public
interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IColumnSelector&lt;/span&gt;&amp;lt;Table, ColumnType&amp;gt;
{ &lt;span style="color: #2b91af"&gt;IChainedSelector&lt;/span&gt;&amp;lt;Table&amp;gt; ForColumn&amp;lt;TColumn&amp;gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;Table,
TColumn&amp;gt; columnSelection) &lt;span style="color: blue"&gt;where &lt;/span&gt;TColumn : &lt;span style="color: #2b91af"&gt;IDatabaseColumn&lt;/span&gt;&amp;lt;ColumnType&amp;gt;;
} &lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IChainedSelector&lt;/span&gt;&amp;lt;Table&amp;gt;
{ &lt;span style="color: #2b91af"&gt;ITableSelector&lt;/span&gt;&amp;lt;Table&amp;gt; And(); &lt;span style="color: #2b91af"&gt;IQuery &lt;/span&gt;End();
}&lt;/pre&gt;
&lt;p&gt;
And here's as far as we got with the implementation...
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TableSelector&lt;/span&gt;&amp;lt;Table&amp;gt;
: &lt;span style="color: #2b91af"&gt;ITableSelector&lt;/span&gt;&amp;lt;Table&amp;gt; &lt;span style="color: blue"&gt;where &lt;/span&gt;Table
: &lt;span style="color: #2b91af"&gt;IDatabaseTable &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IColumnSelector&lt;/span&gt;&amp;lt;Table,
T&amp;gt; ValueOf&amp;lt;T&amp;gt;(T value) { &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ColumnSelector&lt;/span&gt;&amp;lt;Table,
T&amp;gt;(value); } } &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ColumnSelector&lt;/span&gt;&amp;lt;Table,
T&amp;gt; : &lt;span style="color: #2b91af"&gt;IColumnSelector&lt;/span&gt;&amp;lt;Table, T&amp;gt; &lt;span style="color: blue"&gt;where &lt;/span&gt;Table
: &lt;span style="color: #2b91af"&gt;IDatabaseTable &lt;/span&gt;{ &lt;span style="color: blue"&gt;private
readonly &lt;/span&gt;T value; &lt;span style="color: blue"&gt;public &lt;/span&gt;ColumnSelector(T
value) { &lt;span style="color: blue"&gt;this&lt;/span&gt;.value = value; } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IChainedSelector&lt;/span&gt;&amp;lt;Table&amp;gt;
ForColumn&amp;lt;TColumn&amp;gt;(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;Table, TColumn&amp;gt;
columnSelection) &lt;span style="color: blue"&gt;where &lt;/span&gt;TColumn : &lt;span style="color: #2b91af"&gt;IDatabaseColumn&lt;/span&gt;&amp;lt;T&amp;gt;
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;table = &lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance&amp;lt;Table&amp;gt;(); &lt;span style="color: blue"&gt;return
new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ChainedSelector&lt;/span&gt;&amp;lt;Table, T, TColumn&amp;gt;(
table, value, columnSelection(table) ); } } &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ChainedSelector&lt;/span&gt;&amp;lt;Table,
Value, Column&amp;gt; : &lt;span style="color: #2b91af"&gt;IChainedSelector&lt;/span&gt;&amp;lt;Table&amp;gt; &lt;span style="color: blue"&gt;where &lt;/span&gt;Table
: &lt;span style="color: #2b91af"&gt;IDatabaseTable &lt;/span&gt;&lt;span style="color: blue"&gt;where &lt;/span&gt;Column
: &lt;span style="color: #2b91af"&gt;IDatabaseColumn&lt;/span&gt;&amp;lt;Value&amp;gt; { &lt;span style="color: blue"&gt;private
readonly &lt;/span&gt;Table table; &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;Value
value; &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;Column column; &lt;span style="color: blue"&gt;public &lt;/span&gt;ChainedSelector(Table
table, Value value, Column column) { &lt;span style="color: blue"&gt;this&lt;/span&gt;.table =
table; &lt;span style="color: blue"&gt;this&lt;/span&gt;.value = value; &lt;span style="color: blue"&gt;this&lt;/span&gt;.column
= column; } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ITableSelector&lt;/span&gt;&amp;lt;Table&amp;gt;
And() { &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NotImplementedException&lt;/span&gt;();
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQuery &lt;/span&gt;End()
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;builder = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;InsertStatementBuilder&lt;/span&gt;(table.Name());
builder.Add(column, value); &lt;span style="color: blue"&gt;return &lt;/span&gt;builder.EndQuery();
} }&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The most important piece is still
missing, and that's implementing the &amp;quot;And()&amp;quot; method on ChainedSelector...
and finishing off the End method. I'm drawing a blank.. Thoughts are appreciated!
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&amp;#160;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://mokhan.ca/blog/aggbug.ashx?id=3612b693-b263-41da-98f2-3717bb790166" /&gt;</description>
      <category>ADO.NET</category>
      <category>CSharp</category>
    </item>
    <item>
      <trackback:ping>http://mokhan.ca/blog/Trackback.aspx?guid=ebf6bd60-4f47-4ac7-9bd5-b8f68424a4f7</trackback:ping>
      <pingback:server>http://mokhan.ca/blog/pingback.aspx</pingback:server>
      <pingback:target>http://mokhan.ca/blog/PermaLink,guid,ebf6bd60-4f47-4ac7-9bd5-b8f68424a4f7.aspx</pingback:target>
      <dc:creator>Mr mO!</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Here it is my final assignment for CMPP298 - Database Programming ADO.NET. Assignment
is as follows:
</p>
        <blockquote>
          <p>
"Create a Windows Forms application to support data maintenance (CRUD functionality)
of Invoices per Vendor in the Payables database. 
<br />
Make use of the Connection, Transaction, Command, and DataReader objects of the ADO.NET
SQLClient Provider, 
<br />
not DataSets, DataTables, DataAdapters or the Configuration Wizard. 
</p>
          <p>
The user has to select a Vendor and then view, add, edit or delete Invoice records
for the selected vendor. 
<br />
Changes have to be saved before the user can select another vendor. 
</p>
          <p>
Feel free to make use of a combination of UI controls such as Grids, TextBoxes, ComboBoxes,
etc. "
</p>
        </blockquote>
        <p>
The focus of the assignment was on ADO.NET 2.0. So my implementation of validation,
UI, and domain is rather weak. I wanted to focus my attention on learning the core
of ADO.NET 2.0 by learning more about...
</p>
        <ul>
          <li>
            <strong>IDbConnection:</strong> The interface the the abstract DbConnection type implements
that all connection sub classes inherit from for each ado.net provider. (OleDb, Odbc,
Oracle, SQL Server, MySQL) 
</li>
          <li>
            <strong>IDbCommand:</strong> The interface that the abstract DbCommand type implements
that all command objects inherit from. 
</li>
          <li>
            <strong>DataTable:</strong> An in memory type used to store data in a tabular
format. 
</li>
          <li>
            <strong>IDataReader:</strong> A forward only, read-only type that allows you to iterate
through  a result set. 
</li>
          <li>
            <strong>IDataParameter:</strong> This type represents a parameter for a IDbCommand
object. 
</li>
          <li>
            <strong>DbProviderFactories: </strong> Provides a factory method to
return a DbProviderFactory abstract factory with methods such as CreateConnection. 
</li>
        </ul>
        <h2>IDbConnection<a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbConnection.png" atomicselection="true"><img style="border-width: 0px;" alt="IDbConnection" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbConnection_thumb.png" align="right" border="0" height="231" width="286" /></a></h2>
        <p>
The IDbConnection interface demands a set of very simple methods and properties. I
found "CreateCommand()" and "Open()" to be the most useful. Working against the interface
as opposed to the concrete implementation simplified learning for myself. I was able
to focus on the most important traits and behaviors that all Connection types share.
</p>
        <p>
The "CreateCommand()" method returns a "IDbCommand" type that has it's connection
property set to the the IDbConnection type that created it. Further reducing the amount
of code needed to be written.
</p>
        <p>
In my assignment I created and ConnectionFactory that has a single factory method
that returns IDbConnection types. This separates where the connection string settings
are retrieved from, from where the connection object is actually used.
</p>
        <pre class="code">
          <span style="color: rgb(0, 0, 255);">using</span> ( <span style="color: rgb(43, 145, 175);">IDbConnection</span> connection
= _connectionFactory.Create( ) ) { <span style="color: rgb(43, 145, 175);">IDbCommand</span> command
= connection.CreateCommand( ); command.CommandText = sqlQuery; connection.Open( ); <span style="color: rgb(43, 145, 175);">IDataReader</span> reader
= command.ExecuteReader( ); <span style="color: rgb(43, 145, 175);">DataTable</span> table
= <span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(43, 145, 175);">DataTable</span>(
); table.Load( reader ); <span style="color: rgb(0, 0, 255);">return</span> table;
}</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>You might have also noticed that IDbConnection
also implements the IDisposable interface which allows me to use the type in a "using"
block, which implicitly calls the Dispose method on the type. The Dispose method will
then close the connection and clean up any other unmanaged resources.
</p>
        <h2> IDbCommand<a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbCommand.png" atomicselection="true"><img style="border-width: 0px;" alt="IDbCommand" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbCommand_thumb.png" align="right" border="0" height="290" width="323" /></a></h2>
        <p>
Types that implement the IDbCommand interface are used to execute "commands" against
a database. This uses the Command pattern with 3 types of execute methods. 
</p>
        <ul>
          <li>
ExecuteReader: will execute a SQL command against the data source and return
and IDataReader type that allows you to read through a result set. 
</li>
          <li>
ExecuteNonQuery: will execute a SQL command against the data source and returns the
number of rows affected by the command. 
</li>
          <li>
ExecuteScalar: will execute a SQL command against the data source and returns the
value in the first column of the first row.</li>
        </ul>
        <p>
The CommandText property is either the name of the stored procedure or the raw SQL
to execute against the data source.
</p>
        <pre class="code">
          <span style="color: rgb(43, 145, 175);">IDbCommand</span> command
= connection.CreateCommand( ); connection.Open( ); command.CommandText = query; command.ExecuteNonQuery(
);</pre>
        <h2>DataTable
</h2>
        <p>
A DataTable is an in memory container for tabular data. It has a method named "Load()"
that takes in an IDataReader type and will load the data table with the entire result
set from the IDataReader. The overloaded Load method looks like this in Lutz Reflector:
</p>
        <p>
 <a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DataTableLoad.png" atomicselection="true"><img style="border-width: 0px;" alt="DataTableLoad" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DataTableLoad_thumb.png" border="0" height="460" width="647" /></a></p>
        <p>
A client usage of the Load method looks like this:
</p>
        <pre class="code">
          <span style="color: rgb(43, 145, 175);">IDataReader</span> reader
= command.ExecuteReader( ); <span style="color: rgb(43, 145, 175);">DataTable</span> table
= <span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(43, 145, 175);">DataTable</span>(
); table.Load( reader );</pre>
        <h2>
          <a href="http://11011.net/software/vspaste">
          </a> IDataReader
</h2>
        <p>
Types that implement the IDataReader interface allow for forward-only reading
through 1 or more result sets. 
</p>
        <p>
          <a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDataReader.png" atomicselection="true">
            <img style="border-width: 0px;" alt="IDataReader" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDataReader_thumb.png" border="0" height="191" width="298" />
          </a>
        </p>
        <p>
The IDataReader type also inherits from IDataRecord which exposes a set of get methods
like "GetDecimal()", "GetBoolean()", "GetOrdinal()", "GetString()" to read out the
value from each column as you iterate through the result set. This can definitely
lead to some sloppy code such as...
</p>
        <pre class="code">
          <span style="color: rgb(0, 0, 255);">private</span>
          <span style="color: rgb(0, 0, 255);">void</span> SloppyReader( <span style="color: rgb(43, 145, 175);">IDataReader</span> reader
) { <span style="color: rgb(0, 0, 255);">string</span> customerFirstName = !reader.IsDBNull(
1 ) ? reader.GetString( 1 ) : <span style="color: rgb(163, 21, 21);">"Unknown"</span>; <span style="color: rgb(0, 0, 255);">string</span> customerLastName
= !reader.IsDBNull( 2 ) ? reader.GetString( 2 ) : <span style="color: rgb(163, 21, 21);">"Unknown"</span>;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <h2>IDataParameter
</h2>
        <p>
The IDataParameter is a parameter that is used by command objects. For example in
the following SQL syntax @FirstName and @LastName are parameters.
</p>
        <p>
INSERT INTO Customer (FirstName, LastName) VALUES (@FirstName, @LastName);
</p>
        <p>
The following C# will insert the values "Mo" and "Khan" as the @FirstName, and @LastName
parameters.
</p>
        <pre class="code">
          <span style="color: rgb(43, 145, 175);">IDbCommand</span> command
= connection.CreateCommand( ); command.CommandText = <span style="color: rgb(163, 21, 21);">"INSERT
INTO Customer (FirstName, LastName) VALUES (@FirstName, @LastName);"</span>; <span style="color: rgb(43, 145, 175);">IDataParameter</span> commandParameter
= command.CreateParameter( ); commandParameter.ParameterName = <span style="color: rgb(163, 21, 21);">"@FirstName"</span>;
commandParameter.Value = <span style="color: rgb(163, 21, 21);">"Mo"</span>; command.Parameters.Add(
commandParameter ); commandParameter = command.CreateParameter( ); commandParameter.ParameterName
= <span style="color: rgb(163, 21, 21);">"@LastName"</span>; commandParameter.Value
= <span style="color: rgb(163, 21, 21);">"Khan"</span>; command.Parameters.Add( commandParameter
);</pre>
        <h2>DbProviderFactories<a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactories.png" atomicselection="true"><img style="border-width: 0px;" alt="DbProviderFactories" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactories_thumb.png" align="right" border="0" height="255" width="399" /></a></h2>
        <p>
This type has a factory method called "GetFactory()" which returns an abstract factory
for creating connections, commands, adapters and parameters. 
</p>
        <p>
To construct a DbProviderFactory you need to specify the ADO.NET provider to use.
For example for the SQL Server Provider, you would use the invariant provider name
of "System.Data.SqlClient".
</p>
        <p>
This works great with the ConnectionStringSettings section of the *.config file. Using
the ConnectionStringSettings type you can extract the connection string as well as
the provider name and construct a DbProviderFactory without a re-compile.
</p>
        <p>
With a quick change to the *.config I can switch from a SQL Server provider to an
Oracle provider or MySQL provider, or an OleDb provider or and Odbc provider.
</p>
        <pre class="code">
          <span style="color: rgb(0, 0, 255);"> &lt;</span>
          <span style="color: rgb(163, 21, 21);">connectionStrings</span>
          <span style="color: rgb(0, 0, 255);">&gt;
&lt;</span>
          <span style="color: rgb(163, 21, 21);">add</span>
          <span style="color: rgb(0, 0, 255);">
          </span>
          <span style="color: rgb(255, 0, 0);">name</span>
          <span style="color: rgb(0, 0, 255);">=</span>"<span style="color: rgb(0, 0, 255);">PayablesConnection</span>"<span style="color: rgb(0, 0, 255);"></span><span style="color: rgb(255, 0, 0);">connectionString</span><span style="color: rgb(0, 0, 255);">=</span>"<span style="color: rgb(0, 0, 255);">data
source=(local);Integrated Security=SSPI;Initial Catalog=Payables;</span>"<span style="color: rgb(0, 0, 255);"></span><span style="color: rgb(255, 0, 0);">providerName</span><span style="color: rgb(0, 0, 255);">=</span>"<span style="color: rgb(0, 0, 255);">System.Data.SqlClient</span>"<span style="color: rgb(0, 0, 255);"> /&gt;
&lt;/</span><span style="color: rgb(163, 21, 21);">connectionStrings</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
        <pre class="code">
          <span style="color: rgb(0, 0, 255);">
            <a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactory.png" atomicselection="true">
              <img style="border-width: 0px;" alt="DbProviderFactory" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactory_thumb.png" border="0" height="233" width="419" />
            </a>
          </span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 The DbProviderFactory allows you to create database agnostic ADO.NET types through
factory methods.  
</p>
        <pre class="code">
          <span style="color: rgb(0, 0, 255);">public</span> DatabaseConnectionFactory( <span style="color: rgb(43, 145, 175);">ConnectionStringSettings</span> connectionStringSettings
) { _settings = connectionStringSettings; } <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(43, 145, 175);">IDbConnection</span> Create(
) { <span style="color: rgb(43, 145, 175);">IDbConnection</span> connection = <span style="color: rgb(43, 145, 175);">DbProviderFactories</span>.GetFactory(
_settings.ProviderName ).CreateConnection( ); connection.ConnectionString = _settings.ConnectionString; <span style="color: rgb(0, 0, 255);">return</span> connection;
}</pre>
        <p>
ADO.NET seems intimidating at first, but when you break it down piece by piece it's
not such a scary beast. Now none of the examples provided use stored procedures, but
using similar techniques discussed you could transition to stored procedures
with ease and confidence.
</p>
        <p>
Now as for DataSets, DataAdapters, CodeGen and Wizards... that's a whole other story!
</p>
        <a href="http://mokhan.ca/blog/content/binary/Sait.Cmpp298.Assignment3.zip">Sait.Cmpp298.Assignment3.zip
(2.35 MB)</a>
        <img width="0" height="0" src="http://mokhan.ca/blog/aggbug.ashx?id=ebf6bd60-4f47-4ac7-9bd5-b8f68424a4f7" />
      </body>
      <title>ADO.NET 2.0</title>
      <guid isPermaLink="false">http://mokhan.ca/blog/PermaLink,guid,ebf6bd60-4f47-4ac7-9bd5-b8f68424a4f7.aspx</guid>
      <link>http://mokhan.ca/blog/2007/08/26/ADONET+20.aspx</link>
      <pubDate>Sun, 26 Aug 2007 20:10:12 GMT</pubDate>
      <description>&lt;p&gt;
Here it is my final assignment for CMPP298 - Database Programming ADO.NET. Assignment
is as follows:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
"Create a Windows Forms application to support data maintenance (CRUD functionality)
of Invoices per Vendor in the Payables database. 
&lt;br&gt;
Make use of the Connection, Transaction, Command, and DataReader objects of the ADO.NET
SQLClient Provider, 
&lt;br&gt;
not DataSets, DataTables, DataAdapters or the Configuration Wizard. 
&lt;/p&gt;
&lt;p&gt;
The user has to select a Vendor and then view, add, edit or delete Invoice records
for the selected vendor. 
&lt;br&gt;
Changes have to be saved before the user can select another vendor. 
&lt;/p&gt;
&lt;p&gt;
Feel free to make use of a combination of UI controls such as Grids, TextBoxes, ComboBoxes,
etc. "
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The focus of the assignment was on ADO.NET 2.0. So my implementation of&amp;nbsp;validation,
UI, and domain is rather weak. I wanted to focus my attention on learning the core
of ADO.NET 2.0 by learning more about...
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IDbConnection:&lt;/strong&gt; The interface the the abstract DbConnection type implements
that all connection sub classes inherit from for each ado.net provider. (OleDb, Odbc,
Oracle, SQL Server, MySQL) 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IDbCommand:&lt;/strong&gt; The interface that the abstract DbCommand type implements
that all command objects inherit from. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DataTable:&lt;/strong&gt; An in memory&amp;nbsp;type used to store data in a tabular
format. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IDataReader:&lt;/strong&gt; A forward only, read-only type that allows you to iterate
through&amp;nbsp; a result set. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IDataParameter:&lt;/strong&gt; This type represents a parameter for a IDbCommand
object. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DbProviderFactories:&amp;nbsp;&lt;/strong&gt;&amp;nbsp;Provides a&amp;nbsp;factory method to
return&amp;nbsp;a&amp;nbsp;DbProviderFactory abstract factory with methods such as CreateConnection. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;IDbConnection&lt;a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbConnection.png" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="IDbConnection" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbConnection_thumb.png" align="right" border="0" height="231" width="286"&gt;&lt;/a&gt; 
&lt;/h2&gt;
&lt;p&gt;
The IDbConnection interface demands a set of very simple methods and properties. I
found "CreateCommand()" and "Open()" to be the most useful. Working against the interface
as opposed to the concrete implementation simplified learning for myself. I was able
to focus on the most important traits and behaviors that all Connection types share.
&lt;/p&gt;
&lt;p&gt;
The "CreateCommand()" method returns a "IDbCommand" type that has it's connection
property set to the the IDbConnection type that created it. Further reducing the amount
of code needed to be written.
&lt;/p&gt;
&lt;p&gt;
In my assignment I created and ConnectionFactory that has a single factory method
that returns IDbConnection types. This separates where the connection string settings
are retrieved from, from where the connection object is actually used.
&lt;/p&gt;
&lt;pre class="code"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; ( &lt;span style="color: rgb(43, 145, 175);"&gt;IDbConnection&lt;/span&gt; connection
= _connectionFactory.Create( ) ) { &lt;span style="color: rgb(43, 145, 175);"&gt;IDbCommand&lt;/span&gt; command
= connection.CreateCommand( ); command.CommandText = sqlQuery; connection.Open( ); &lt;span style="color: rgb(43, 145, 175);"&gt;IDataReader&lt;/span&gt; reader
= command.ExecuteReader( ); &lt;span style="color: rgb(43, 145, 175);"&gt;DataTable&lt;/span&gt; table
= &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;DataTable&lt;/span&gt;(
); table.Load( reader ); &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; table;
}&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;You might have also noticed that IDbConnection
also implements the IDisposable interface which allows me to use the type in a "using"
block, which implicitly calls the Dispose method on the type. The Dispose method will
then close the connection and clean up any other unmanaged resources.
&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;IDbCommand&lt;a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbCommand.png" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="IDbCommand" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDbCommand_thumb.png" align="right" border="0" height="290" width="323"&gt;&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;
Types that implement the IDbCommand interface are used to execute "commands" against
a database. This uses the Command pattern with 3 types of&amp;nbsp;execute methods. 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
ExecuteReader: will execute a&amp;nbsp;SQL&amp;nbsp;command against the data source and&amp;nbsp;return
and IDataReader type that allows you to read through a result set. 
&lt;/li&gt;
&lt;li&gt;
ExecuteNonQuery: will execute a SQL command against the data source and returns the
number of rows affected by the command. 
&lt;/li&gt;
&lt;li&gt;
ExecuteScalar: will execute a SQL command against the data source and returns the
value in the first column of the first row.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The CommandText property is either the name of the stored procedure or the raw SQL
to execute against the data source.
&lt;/p&gt;
&lt;pre class="code"&gt;                &lt;span style="color: rgb(43, 145, 175);"&gt;IDbCommand&lt;/span&gt; command
= connection.CreateCommand( ); connection.Open( ); command.CommandText = query; command.ExecuteNonQuery(
);&lt;/pre&gt;
&lt;h2&gt;DataTable
&lt;/h2&gt;
&lt;p&gt;
A DataTable is an in memory container for tabular data. It has a method named "Load()"
that takes in an IDataReader type and will load the data table with the entire result
set from the IDataReader. The overloaded Load method looks like this in Lutz Reflector:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&lt;a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DataTableLoad.png" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="DataTableLoad" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DataTableLoad_thumb.png" border="0" height="460" width="647"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
A client usage of the Load method looks like this:
&lt;/p&gt;
&lt;pre class="code"&gt;                &lt;span style="color: rgb(43, 145, 175);"&gt;IDataReader&lt;/span&gt; reader
= command.ExecuteReader( ); &lt;span style="color: rgb(43, 145, 175);"&gt;DataTable&lt;/span&gt; table
= &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;DataTable&lt;/span&gt;(
); table.Load( reader );&lt;/pre&gt;
&lt;h2&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&amp;nbsp;IDataReader
&lt;/h2&gt;
&lt;p&gt;
Types that implement the IDataReader interface allow for&amp;nbsp;forward-only reading
through&amp;nbsp;1 or more&amp;nbsp;result sets. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDataReader.png" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="IDataReader" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/IDataReader_thumb.png" border="0" height="191" width="298"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The IDataReader type also inherits from IDataRecord which exposes a set of get methods
like "GetDecimal()", "GetBoolean()", "GetOrdinal()", "GetString()" to read out the
value from each column as you iterate through the result set. This can definitely
lead to some sloppy code such as...
&lt;/p&gt;
&lt;pre class="code"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; SloppyReader( &lt;span style="color: rgb(43, 145, 175);"&gt;IDataReader&lt;/span&gt; reader
) { &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; customerFirstName = !reader.IsDBNull(
1 ) ? reader.GetString( 1 ) : &lt;span style="color: rgb(163, 21, 21);"&gt;"Unknown"&lt;/span&gt;; &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; customerLastName
= !reader.IsDBNull( 2 ) ? reader.GetString( 2 ) : &lt;span style="color: rgb(163, 21, 21);"&gt;"Unknown"&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;h2&gt;IDataParameter
&lt;/h2&gt;
&lt;p&gt;
The IDataParameter is a parameter that is used by command objects. For example&amp;nbsp;in
the following SQL syntax @FirstName and @LastName are parameters.
&lt;/p&gt;
&lt;p&gt;
INSERT INTO Customer (FirstName, LastName) VALUES (@FirstName, @LastName);
&lt;/p&gt;
&lt;p&gt;
The following C# will insert the values "Mo" and "Khan" as the @FirstName, and @LastName
parameters.
&lt;/p&gt;
&lt;pre class="code"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;IDbCommand&lt;/span&gt; command
= connection.CreateCommand( ); command.CommandText = &lt;span style="color: rgb(163, 21, 21);"&gt;"INSERT
INTO Customer (FirstName, LastName) VALUES (@FirstName, @LastName);"&lt;/span&gt;; &lt;span style="color: rgb(43, 145, 175);"&gt;IDataParameter&lt;/span&gt; commandParameter
= command.CreateParameter( ); commandParameter.ParameterName = &lt;span style="color: rgb(163, 21, 21);"&gt;"@FirstName"&lt;/span&gt;;
commandParameter.Value = &lt;span style="color: rgb(163, 21, 21);"&gt;"Mo"&lt;/span&gt;; command.Parameters.Add(
commandParameter ); commandParameter = command.CreateParameter( ); commandParameter.ParameterName
= &lt;span style="color: rgb(163, 21, 21);"&gt;"@LastName"&lt;/span&gt;; commandParameter.Value
= &lt;span style="color: rgb(163, 21, 21);"&gt;"Khan"&lt;/span&gt;; command.Parameters.Add( commandParameter
);&lt;/pre&gt;
&lt;h2&gt;DbProviderFactories&lt;a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactories.png" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="DbProviderFactories" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactories_thumb.png" align="right" border="0" height="255" width="399"&gt;&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;
This type has a factory method called "GetFactory()" which returns an abstract factory
for creating connections, commands, adapters and parameters. 
&lt;/p&gt;
&lt;p&gt;
To construct a DbProviderFactory you need to specify the ADO.NET provider to use.
For example for the SQL Server Provider, you would use the invariant provider name
of "System.Data.SqlClient".
&lt;/p&gt;
&lt;p&gt;
This works great with the ConnectionStringSettings section of the *.config file. Using
the ConnectionStringSettings type you can extract the connection string as well as
the provider name and construct a DbProviderFactory without a re-compile.
&lt;/p&gt;
&lt;p&gt;
With a quick change to the *.config I can switch from a SQL Server provider to an
Oracle provider or MySQL provider, or an OleDb provider or and Odbc provider.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;add&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;PayablesConnection&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;connectionString&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;data
source=(local);Integrated Security=SSPI;Initial Catalog=Payables;&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;providerName&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;System.Data.SqlClient&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&lt;a href="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactory.png" atomicselection="true"&gt;&lt;img style="border-width: 0px;" alt="DbProviderFactory" src="http://mokhan.ca/blog/content/binary/WindowsLiveWriter/a5b20965017e_CA2A/DbProviderFactory_thumb.png" border="0" height="233" width="419"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;nbsp;The DbProviderFactory allows you to create database agnostic ADO.NET types through
factory methods.&amp;nbsp; 
&lt;/p&gt;
&lt;pre class="code"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; DatabaseConnectionFactory( &lt;span style="color: rgb(43, 145, 175);"&gt;ConnectionStringSettings&lt;/span&gt; connectionStringSettings
) { _settings = connectionStringSettings; } &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IDbConnection&lt;/span&gt; Create(
) { &lt;span style="color: rgb(43, 145, 175);"&gt;IDbConnection&lt;/span&gt; connection = &lt;span style="color: rgb(43, 145, 175);"&gt;DbProviderFactories&lt;/span&gt;.GetFactory(
_settings.ProviderName ).CreateConnection( ); connection.ConnectionString = _settings.ConnectionString; &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; connection;
}&lt;/pre&gt;
&lt;p&gt;
ADO.NET seems intimidating at first, but when you break it down piece by piece it's
not such a scary beast. Now none of the examples provided use stored procedures, but
using&amp;nbsp;similar techniques discussed you could transition to stored procedures
with ease and confidence.
&lt;/p&gt;
&lt;p&gt;
Now as for DataSets, DataAdapters, CodeGen and Wizards... that's a whole other story!
&lt;/p&gt;
&lt;a href="http://mokhan.ca/blog/content/binary/Sait.Cmpp298.Assignment3.zip"&gt;Sait.Cmpp298.Assignment3.zip
(2.35 MB)&lt;/a&gt;&lt;img width="0" height="0" src="http://mokhan.ca/blog/aggbug.ashx?id=ebf6bd60-4f47-4ac7-9bd5-b8f68424a4f7" /&gt;</description>
      <category>ADO.NET</category>
      <category>CSharp</category>
      <category>SAIT</category>
    </item>
  </channel>
</rss>