Skip to content

Commit

Permalink
Reading Rows Using a Query with No Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahi committed Oct 3, 2021
1 parent 944c0c6 commit 02cf0b0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
34 changes: 33 additions & 1 deletion daab/a00940.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@
<li class="level2"><a href="#autotoc_md3">Configuration</a><ul><li class="level3"><a href="#autotoc_md4">Oracle Configuration</a></li>
</ul>
</li>
<li class="level2"><a href="#autotoc_md5">Code Examples</a></li>
<li class="level2"><a href="#autotoc_md5">Code Examples</a><ul><li class="level3"><a href="#autotoc_md6">Creating Database Instances</a></li>
<li class="level3"><a href="#autotoc_md7">Reading Rows Using a Query with No Parameters</a></li>
</ul>
</li>
</ul>
</ul>
</div>
Expand Down Expand Up @@ -186,6 +189,8 @@ <h3><a class="anchor" id="autotoc_md4"></a>
</ol>
<h2><a class="anchor" id="autotoc_md5"></a>
Code Examples</h2>
<h3><a class="anchor" id="autotoc_md6"></a>
Creating Database Instances</h3>
<p>The simplest approach for creating a <code>Database</code> object or one of its descendants is calling the <code>CreateDefault</code> or <code>Create</code> method of the <code>DatabaseProviderFactory</code> class, as shown here, and storing these instances in application-wide variables so that they can be accessed from anywhere in the code. </p><div class="fragment"><div class="line"><span class="comment">// Configure the DatabaseFactory to read its configuration from the .config file</span></div>
<div class="line">DatabaseProviderFactory factory = <span class="keyword">new</span> DatabaseProviderFactory();</div>
<div class="line"> </div>
Expand All @@ -204,6 +209,33 @@ <h2><a class="anchor" id="autotoc_md5"></a>
<div class="line">sqlServerDB = DatabaseFactory.CreateDatabase() as SqlDatabase;</div>
</div><!-- fragment --><p>In addition to using configuration to define the databases you will use, the Data Access block allows you to create instances of concrete types that inherit from the Database class directly in your code, as shown here.</p>
<div class="fragment"><div class="line">SqlDatabase sqlDatabase = <span class="keyword">new</span> SqlDatabase(myConnectionString);</div>
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md7"></a>
Reading Rows Using a Query with No Parameters</h3>
<p>To read rows using a stored procedure with no parameters, retrieve an <code>IDataReader</code> and read values from it: </p><div class="fragment"><div class="line"><span class="comment">// Call the ExecuteReader method by specifying just the stored procedure name.</span></div>
<div class="line"><span class="keyword">using</span> (IDataReader reader = namedDB.ExecuteReader(<span class="stringliteral">&quot;MyStoredProcName&quot;</span>))</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// Use the values in the rows as required.</span></div>
<div class="line"> DisplayRowValues(reader);</div>
<div class="line">}</div>
</div><!-- fragment --><p> <code>ExecuteReader</code> also accept a <code>CommandType</code>, but the default value is <code>CommandType.StoredProcedure</code>.</p>
<p>To use an inline SQL statement, specify a <code>CommandType.Text</code>: </p><div class="fragment"><div class="line"><span class="comment">// Call the ExecuteReader method by specifying the command type</span></div>
<div class="line"><span class="comment">// as a SQL statement, and passing in the SQL statement.</span></div>
<div class="line"><span class="keyword">using</span> (IDataReader reader = namedDB.ExecuteReader(CommandType.Text, <span class="stringliteral">&quot;SELECT TOP 1 * FROM OrderList&quot;</span>))</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// Use the values in the rows as required - here we are just displaying them.</span></div>
<div class="line"> DisplayRowValues(reader);</div>
<div class="line">}</div>
</div><!-- fragment --><div class="fragment"><div class="line"><span class="keyword">private</span> <span class="keywordtype">void</span> DisplayRowValues(IDataReader reader)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordflow">while</span> (reader.Read())</div>
<div class="line"> {</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; reader.FieldCount; i++)</div>
<div class="line"> {</div>
<div class="line"> Console.WriteLine(<span class="stringliteral">&quot;{0} = {1}&quot;</span>, reader.GetName(i), reader[i].ToString());</div>
<div class="line"> }</div>
<div class="line"> Console.WriteLine();</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
Expand Down
5 changes: 4 additions & 1 deletion daab/annotated_dup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ var annotated_dup =
[ "Configuration", "a00940.html#autotoc_md3", [
[ "Oracle Configuration", "a00940.html#autotoc_md4", null ]
] ],
[ "Code Examples", "a00940.html#autotoc_md5", null ],
[ "Code Examples", "a00940.html#autotoc_md5", [
[ "Creating Database Instances", "a00940.html#autotoc_md6", null ],
[ "Reading Rows Using a Query with No Parameters", "a00940.html#autotoc_md7", null ]
] ],
[ "Microsoft", "a00233.html", [
[ "Practices", "a00234.html", [
[ "EnterpriseLibrary", "a00235.html", [
Expand Down
2 changes: 1 addition & 1 deletion daab/navtreedata.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var NAVTREEINDEX =
"a00523.html#a9f8ee827a130d35f3cd1ae2b28eb0310",
"a00615.html#aec4e078b46f775b90d01038bab518c57",
"a00695.html#a27469f9c1ac7f0c4d1c86fdfc3000b0e",
"functions_func_g.html"
"functions_func_e.html"
];

var SYNCONMSG = 'click to disable panel synchronisation';
Expand Down
6 changes: 3 additions & 3 deletions daab/navtreeindex4.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ var NAVTREEINDEX4 =
"a00940.html#autotoc_md3":[2],
"a00940.html#autotoc_md4":[2,0],
"a00940.html#autotoc_md5":[3],
"a00940.html#autotoc_md6":[3,0],
"a00940.html#autotoc_md7":[3,1],
"annotated.html":[1,0],
"classes.html":[1,1],
"functions.html":[1,3,0,0],
Expand All @@ -247,7 +249,5 @@ var NAVTREEINDEX4 =
"functions_func.html":[1,3,1],
"functions_func_b.html":[1,3,1,1],
"functions_func_c.html":[1,3,1,2],
"functions_func_d.html":[1,3,1,3],
"functions_func_e.html":[1,3,1,4],
"functions_func_f.html":[1,3,1,5]
"functions_func_d.html":[1,3,1,3]
};
2 changes: 2 additions & 0 deletions daab/navtreeindex5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var NAVTREEINDEX5 =
{
"functions_func_e.html":[1,3,1,4],
"functions_func_f.html":[1,3,1,5],
"functions_func_g.html":[1,3,1,6],
"functions_func_i.html":[1,3,1,7],
"functions_func_l.html":[1,3,1,8],
Expand Down

0 comments on commit 02cf0b0

Please sign in to comment.