Skip to content

Commit

Permalink
Reading Rows Using a Query with named Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahi committed Oct 5, 2021
1 parent d443766 commit df09149
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
33 changes: 33 additions & 0 deletions daab/a00940.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<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>
<li class="level3"><a href="#autotoc_md8">Reading Rows Using a Query with unnamed Parameters</a></li>
<li class="level3"><a href="#autotoc_md9">Reading Rows Using a Query with named Parameters</a></li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -246,6 +247,38 @@ <h3><a class="anchor" id="autotoc_md6"></a>
<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 --><h3><a class="anchor" id="autotoc_md9"></a>
Reading Rows Using a Query with named Parameters</h3>
<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>
<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>
<div class="line"> </div>
<div class="line"><span class="comment">// Create a suitable command type and add the required parameter.</span></div>
<div class="line"><span class="keyword">using</span> (DbCommand sqlCmd = defaultDB.GetSqlStringCommand(sqlStatement))</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// Any required prefix, such as &#39;@&#39; or &#39;:&#39; will be added automatically if missing.</span></div>
<div class="line"> defaultDB.AddInParameter(sqlCmd, <span class="stringliteral">&quot;state&quot;</span>, DbType.String, <span class="stringliteral">&quot;New York&quot;</span>);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Call the ExecuteReader method with the command.</span></div>
<div class="line"> <span class="keyword">using</span> (IDataReader sqlReader = defaultDB.ExecuteReader(sqlCmd))</div>
<div class="line"> {</div>
<div class="line"> DisplayRowValues(sqlReader);</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Now read the same data with a stored procedure that accepts one parameter.</span></div>
<div class="line"><span class="keywordtype">string</span> storedProcName = <span class="stringliteral">&quot;ListOrdersByState&quot;</span>;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Create a suitable command type and add the required parameter.</span></div>
<div class="line"><span class="keyword">using</span> (DbCommand sprocCmd = defaultDB.GetStoredProcCommand(storedProcName))</div>
<div class="line">{</div>
<div class="line"> defaultDB.AddInParameter(sprocCmd, <span class="stringliteral">&quot;state&quot;</span>, DbType.String, <span class="stringliteral">&quot;New York&quot;</span>);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Call the ExecuteReader method with the command.</span></div>
<div class="line"> <span class="keyword">using</span> (IDataReader sprocReader = defaultDB.ExecuteReader(sprocCmd))</div>
<div class="line"> {</div>
<div class="line"> DisplayRowValues(sprocReader);</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
Expand Down
3 changes: 2 additions & 1 deletion daab/annotated_dup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var annotated_dup =
[ "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 ],
[ "Reading Rows Using a Query with unnamed Parameters", "a00940.html#autotoc_md8", null ]
[ "Reading Rows Using a Query with unnamed Parameters", "a00940.html#autotoc_md8", null ],
[ "Reading Rows Using a Query with named Parameters", "a00940.html#autotoc_md9", null ]
] ],
[ "Microsoft", "a00233.html", [
[ "Practices", "a00234.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_d.html"
"functions_func_c.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 @@ -237,17 +237,17 @@ var NAVTREEINDEX4 =
"a00940.html#autotoc_md6":[3,0],
"a00940.html#autotoc_md7":[3,1],
"a00940.html#autotoc_md8":[3,2],
"a00940.html#autotoc_md9":[3,3],
"annotated.html":[1,0],
"classes.html":[1,1],
"functions.html":[1,3,0],
"functions.html":[1,3,0,0],
"functions.html":[1,3,0],
"functions_b.html":[1,3,0,1],
"functions_c.html":[1,3,0,2],
"functions_d.html":[1,3,0,3],
"functions_e.html":[1,3,0,4],
"functions_f.html":[1,3,0,5],
"functions_func.html":[1,3,1,0],
"functions_func.html":[1,3,1],
"functions_func_b.html":[1,3,1,1],
"functions_func_c.html":[1,3,1,2]
"functions_func_b.html":[1,3,1,1]
};
1 change: 1 addition & 0 deletions daab/navtreeindex5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var NAVTREEINDEX5 =
{
"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],
Expand Down

0 comments on commit df09149

Please sign in to comment.