Skip to content

Commit df09149

Browse files
committed
Reading Rows Using a Query with named Parameters
#2
1 parent d443766 commit df09149

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

daab/a00940.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<li class="level2"><a href="#autotoc_md5">Code Examples</a><ul><li class="level3"><a href="#autotoc_md6">Creating Database Instances</a></li>
9898
<li class="level3"><a href="#autotoc_md7">Reading Rows Using a Query with No Parameters</a></li>
9999
<li class="level3"><a href="#autotoc_md8">Reading Rows Using a Query with unnamed Parameters</a></li>
100+
<li class="level3"><a href="#autotoc_md9">Reading Rows Using a Query with named Parameters</a></li>
100101
</ul>
101102
</li>
102103
</ul>
@@ -246,6 +247,38 @@ <h3><a class="anchor" id="autotoc_md6"></a>
246247
<div class="line"> <span class="comment">// Use the values in the rows as required - here we are just displaying them.</span></div>
247248
<div class="line"> DisplayRowValues(reader);</div>
248249
<div class="line">}</div>
250+
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md9"></a>
251+
Reading Rows Using a Query with named Parameters</h3>
252+
<p>If you need to specify the data-types of the parameters, e.g. if it can't be inferred from the .NET data type, or you need to specify the parameter direction (input or output), you can access the provider independent <code>DbCommand</code> object for the query and add parameters using methods on the <code>Database</code> object. You can add parameters with a specific direction using the <code>AddInParameter</code> or <code>AddOutParameter</code> method, or by using the <code>AddParameter</code> method and providing a value for the <code>ParameterDirection</code> parameter. You can change the value of existing parameters already added to the command using the <code>GetParameterValue</code> and <code>SetParameterValue</code> methods. </p><div class="fragment"><div class="line"><span class="comment">// Read data with a SQL statement that accepts one parameter prefixed with @.</span></div>
253+
<div class="line"><span class="keywordtype">string</span> sqlStatement = <span class="stringliteral">&quot;SELECT TOP 1 * FROM OrderList WHERE State LIKE @state&quot;</span>;</div>
254+
<div class="line"> </div>
255+
<div class="line"><span class="comment">// Create a suitable command type and add the required parameter.</span></div>
256+
<div class="line"><span class="keyword">using</span> (DbCommand sqlCmd = defaultDB.GetSqlStringCommand(sqlStatement))</div>
257+
<div class="line">{</div>
258+
<div class="line"> <span class="comment">// Any required prefix, such as &#39;@&#39; or &#39;:&#39; will be added automatically if missing.</span></div>
259+
<div class="line"> defaultDB.AddInParameter(sqlCmd, <span class="stringliteral">&quot;state&quot;</span>, DbType.String, <span class="stringliteral">&quot;New York&quot;</span>);</div>
260+
<div class="line"> </div>
261+
<div class="line"> <span class="comment">// Call the ExecuteReader method with the command.</span></div>
262+
<div class="line"> <span class="keyword">using</span> (IDataReader sqlReader = defaultDB.ExecuteReader(sqlCmd))</div>
263+
<div class="line"> {</div>
264+
<div class="line"> DisplayRowValues(sqlReader);</div>
265+
<div class="line"> }</div>
266+
<div class="line">}</div>
267+
<div class="line"> </div>
268+
<div class="line"><span class="comment">// Now read the same data with a stored procedure that accepts one parameter.</span></div>
269+
<div class="line"><span class="keywordtype">string</span> storedProcName = <span class="stringliteral">&quot;ListOrdersByState&quot;</span>;</div>
270+
<div class="line"> </div>
271+
<div class="line"><span class="comment">// Create a suitable command type and add the required parameter.</span></div>
272+
<div class="line"><span class="keyword">using</span> (DbCommand sprocCmd = defaultDB.GetStoredProcCommand(storedProcName))</div>
273+
<div class="line">{</div>
274+
<div class="line"> defaultDB.AddInParameter(sprocCmd, <span class="stringliteral">&quot;state&quot;</span>, DbType.String, <span class="stringliteral">&quot;New York&quot;</span>);</div>
275+
<div class="line"> </div>
276+
<div class="line"> <span class="comment">// Call the ExecuteReader method with the command.</span></div>
277+
<div class="line"> <span class="keyword">using</span> (IDataReader sprocReader = defaultDB.ExecuteReader(sprocCmd))</div>
278+
<div class="line"> {</div>
279+
<div class="line"> DisplayRowValues(sprocReader);</div>
280+
<div class="line"> }</div>
281+
<div class="line">}</div>
249282
</div><!-- fragment --> </div></div><!-- contents -->
250283
</div><!-- PageDoc -->
251284
</div><!-- doc-content -->

daab/annotated_dup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var annotated_dup =
88
[ "Code Examples", "a00940.html#autotoc_md5", [
99
[ "Creating Database Instances", "a00940.html#autotoc_md6", null ],
1010
[ "Reading Rows Using a Query with No Parameters", "a00940.html#autotoc_md7", null ],
11-
[ "Reading Rows Using a Query with unnamed Parameters", "a00940.html#autotoc_md8", null ]
11+
[ "Reading Rows Using a Query with unnamed Parameters", "a00940.html#autotoc_md8", null ],
12+
[ "Reading Rows Using a Query with named Parameters", "a00940.html#autotoc_md9", null ]
1213
] ],
1314
[ "Microsoft", "a00233.html", [
1415
[ "Practices", "a00234.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_d.html"
50+
"functions_func_c.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
@@ -237,17 +237,17 @@ var NAVTREEINDEX4 =
237237
"a00940.html#autotoc_md6":[3,0],
238238
"a00940.html#autotoc_md7":[3,1],
239239
"a00940.html#autotoc_md8":[3,2],
240+
"a00940.html#autotoc_md9":[3,3],
240241
"annotated.html":[1,0],
241242
"classes.html":[1,1],
242-
"functions.html":[1,3,0],
243243
"functions.html":[1,3,0,0],
244+
"functions.html":[1,3,0],
244245
"functions_b.html":[1,3,0,1],
245246
"functions_c.html":[1,3,0,2],
246247
"functions_d.html":[1,3,0,3],
247248
"functions_e.html":[1,3,0,4],
248249
"functions_f.html":[1,3,0,5],
249250
"functions_func.html":[1,3,1,0],
250251
"functions_func.html":[1,3,1],
251-
"functions_func_b.html":[1,3,1,1],
252-
"functions_func_c.html":[1,3,1,2]
252+
"functions_func_b.html":[1,3,1,1]
253253
};

daab/navtreeindex5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var NAVTREEINDEX5 =
22
{
3+
"functions_func_c.html":[1,3,1,2],
34
"functions_func_d.html":[1,3,1,3],
45
"functions_func_e.html":[1,3,1,4],
56
"functions_func_f.html":[1,3,1,5],

0 commit comments

Comments
 (0)