|
97 | 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 | 98 | <li class="level3"><a href="#autotoc_md7">Reading Rows Using a Query with No Parameters</a></li>
|
99 | 99 | <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> |
100 | 101 | </ul>
|
101 | 102 | </li>
|
102 | 103 | </ul>
|
@@ -246,6 +247,38 @@ <h3><a class="anchor" id="autotoc_md6"></a>
|
246 | 247 | <div class="line"> <span class="comment">// Use the values in the rows as required - here we are just displaying them.</span></div>
|
247 | 248 | <div class="line"> DisplayRowValues(reader);</div>
|
248 | 249 | <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">"SELECT TOP 1 * FROM OrderList WHERE State LIKE @state"</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 '@' or ':' will be added automatically if missing.</span></div> |
| 259 | +<div class="line"> defaultDB.AddInParameter(sqlCmd, <span class="stringliteral">"state"</span>, DbType.String, <span class="stringliteral">"New York"</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">"ListOrdersByState"</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">"state"</span>, DbType.String, <span class="stringliteral">"New York"</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> |
249 | 282 | </div><!-- fragment --> </div></div><!-- contents -->
|
250 | 283 | </div><!-- PageDoc -->
|
251 | 284 | </div><!-- doc-content -->
|
|
0 commit comments