You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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><divclass="fragment"><divclass="line"><spanclass="comment">// Configure the DatabaseFactory to read its configuration from the .config file</span></div>
<divclass="line">sqlServerDB = DatabaseFactory.CreateDatabase() as SqlDatabase;</div>
205
210
</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><!-- fragment --><h3><aclass="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><divclass="fragment"><divclass="line"><spanclass="comment">// Call the ExecuteReader method by specifying just the stored procedure name.</span></div>
<divclass="line"><spanclass="comment">// Use the values in the rows as required.</span></div>
218
+
<divclass="line"> DisplayRowValues(reader);</div>
219
+
<divclass="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><divclass="fragment"><divclass="line"><spanclass="comment">// Call the ExecuteReader method by specifying the command type</span></div>
222
+
<divclass="line"><spanclass="comment">// as a SQL statement, and passing in the SQL statement.</span></div>
223
+
<divclass="line"><spanclass="keyword">using</span> (IDataReader reader = namedDB.ExecuteReader(CommandType.Text, <spanclass="stringliteral">"SELECT TOP 1 * FROM OrderList"</span>))</div>
224
+
<divclass="line">{</div>
225
+
<divclass="line"><spanclass="comment">// Use the values in the rows as required - here we are just displaying them.</span></div>
226
+
<divclass="line"> DisplayRowValues(reader);</div>
227
+
<divclass="line">}</div>
228
+
</div><!-- fragment --><divclass="fragment"><divclass="line"><spanclass="keyword">private</span><spanclass="keywordtype">void</span> DisplayRowValues(IDataReader reader)</div>
0 commit comments