Skip to content

Commit 02cf0b0

Browse files
committed
Reading Rows Using a Query with No Parameters
#2
1 parent 944c0c6 commit 02cf0b0

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

daab/a00940.html

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@
9494
<li class="level2"><a href="#autotoc_md3">Configuration</a><ul><li class="level3"><a href="#autotoc_md4">Oracle Configuration</a></li>
9595
</ul>
9696
</li>
97-
<li class="level2"><a href="#autotoc_md5">Code Examples</a></li>
97+
<li class="level2"><a href="#autotoc_md5">Code Examples</a><ul><li class="level3"><a href="#autotoc_md6">Creating Database Instances</a></li>
98+
<li class="level3"><a href="#autotoc_md7">Reading Rows Using a Query with No Parameters</a></li>
99+
</ul>
100+
</li>
98101
</ul>
99102
</ul>
100103
</div>
@@ -186,6 +189,8 @@ <h3><a class="anchor" id="autotoc_md4"></a>
186189
</ol>
187190
<h2><a class="anchor" id="autotoc_md5"></a>
188191
Code Examples</h2>
192+
<h3><a class="anchor" id="autotoc_md6"></a>
193+
Creating Database Instances</h3>
189194
<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>
190195
<div class="line">DatabaseProviderFactory factory = <span class="keyword">new</span> DatabaseProviderFactory();</div>
191196
<div class="line"> </div>
@@ -204,6 +209,33 @@ <h2><a class="anchor" id="autotoc_md5"></a>
204209
<div class="line">sqlServerDB = DatabaseFactory.CreateDatabase() as SqlDatabase;</div>
205210
</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>
206211
<div class="fragment"><div class="line">SqlDatabase sqlDatabase = <span class="keyword">new</span> SqlDatabase(myConnectionString);</div>
212+
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md7"></a>
213+
Reading Rows Using a Query with No Parameters</h3>
214+
<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>
215+
<div class="line"><span class="keyword">using</span> (IDataReader reader = namedDB.ExecuteReader(<span class="stringliteral">&quot;MyStoredProcName&quot;</span>))</div>
216+
<div class="line">{</div>
217+
<div class="line"> <span class="comment">// Use the values in the rows as required.</span></div>
218+
<div class="line"> DisplayRowValues(reader);</div>
219+
<div class="line">}</div>
220+
</div><!-- fragment --><p> <code>ExecuteReader</code> also accept a <code>CommandType</code>, but the default value is <code>CommandType.StoredProcedure</code>.</p>
221+
<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>
222+
<div class="line"><span class="comment">// as a SQL statement, and passing in the SQL statement.</span></div>
223+
<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>
224+
<div class="line">{</div>
225+
<div class="line"> <span class="comment">// Use the values in the rows as required - here we are just displaying them.</span></div>
226+
<div class="line"> DisplayRowValues(reader);</div>
227+
<div class="line">}</div>
228+
</div><!-- fragment --><div class="fragment"><div class="line"><span class="keyword">private</span> <span class="keywordtype">void</span> DisplayRowValues(IDataReader reader)</div>
229+
<div class="line">{</div>
230+
<div class="line"> <span class="keywordflow">while</span> (reader.Read())</div>
231+
<div class="line"> {</div>
232+
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; reader.FieldCount; i++)</div>
233+
<div class="line"> {</div>
234+
<div class="line"> Console.WriteLine(<span class="stringliteral">&quot;{0} = {1}&quot;</span>, reader.GetName(i), reader[i].ToString());</div>
235+
<div class="line"> }</div>
236+
<div class="line"> Console.WriteLine();</div>
237+
<div class="line"> }</div>
238+
<div class="line">}</div>
207239
</div><!-- fragment --> </div></div><!-- contents -->
208240
</div><!-- PageDoc -->
209241
</div><!-- doc-content -->

daab/annotated_dup.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ var annotated_dup =
55
[ "Configuration", "a00940.html#autotoc_md3", [
66
[ "Oracle Configuration", "a00940.html#autotoc_md4", null ]
77
] ],
8-
[ "Code Examples", "a00940.html#autotoc_md5", null ],
8+
[ "Code Examples", "a00940.html#autotoc_md5", [
9+
[ "Creating Database Instances", "a00940.html#autotoc_md6", null ],
10+
[ "Reading Rows Using a Query with No Parameters", "a00940.html#autotoc_md7", null ]
11+
] ],
912
[ "Microsoft", "a00233.html", [
1013
[ "Practices", "a00234.html", [
1114
[ "EnterpriseLibrary", "a00235.html", [

daab/navtreedata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var NAVTREEINDEX =
4747
"a00523.html#a9f8ee827a130d35f3cd1ae2b28eb0310",
4848
"a00615.html#aec4e078b46f775b90d01038bab518c57",
4949
"a00695.html#a27469f9c1ac7f0c4d1c86fdfc3000b0e",
50-
"functions_func_g.html"
50+
"functions_func_e.html"
5151
];
5252

5353
var SYNCONMSG = 'click to disable panel synchronisation';

daab/navtreeindex4.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ var NAVTREEINDEX4 =
234234
"a00940.html#autotoc_md3":[2],
235235
"a00940.html#autotoc_md4":[2,0],
236236
"a00940.html#autotoc_md5":[3],
237+
"a00940.html#autotoc_md6":[3,0],
238+
"a00940.html#autotoc_md7":[3,1],
237239
"annotated.html":[1,0],
238240
"classes.html":[1,1],
239241
"functions.html":[1,3,0,0],
@@ -247,7 +249,5 @@ var NAVTREEINDEX4 =
247249
"functions_func.html":[1,3,1],
248250
"functions_func_b.html":[1,3,1,1],
249251
"functions_func_c.html":[1,3,1,2],
250-
"functions_func_d.html":[1,3,1,3],
251-
"functions_func_e.html":[1,3,1,4],
252-
"functions_func_f.html":[1,3,1,5]
252+
"functions_func_d.html":[1,3,1,3]
253253
};

daab/navtreeindex5.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var NAVTREEINDEX5 =
22
{
3+
"functions_func_e.html":[1,3,1,4],
4+
"functions_func_f.html":[1,3,1,5],
35
"functions_func_g.html":[1,3,1,6],
46
"functions_func_i.html":[1,3,1,7],
57
"functions_func_l.html":[1,3,1,8],

0 commit comments

Comments
 (0)