Using the ExecuteReader<T>(ParameterizedRecordReadEventHandler) Method
While the ExecuteEnumerator() method is preferred for querying data, there are cases where it does not provide the same level of functionality as accessing a
DbDataReader object directly. Accessing Binary data, UDT values or provider specific data types are all situations where the ExecuteReader methods are appropriate.
In this example we're using the parameterized version of ExecuteReader. Here, we simply use the delegate method to retrieve values from the underlying SqlDataReader, create an
object and add it to a collection.
1: public static FaqItemCollection GetFaqByCategory(Lookup<byte> category) {
5: FaqItemCollection collection = new FaqItemCollection();
6:
7: using (SqlConnection connection = new SqlConnection(ApplicationState.ConnectionString)) {
8: using (SelectFaqByCategoryIdAdapter adapter = new SelectFaqByCategoryIdAdapter(connection)) {
9: adapter.CategoryId = category.PrimaryKey.Value;
10: adapter.ExecuteReader<FaqItemCollection>(PersistRecord, collection);
11: }
12: }
13:
14: return collection;
15: }
16:
17: public static void PersistRecord(object sender, SqlDataReader reader, FaqItemCollection stateData) {
18: FaqItem tempFaq = new FaqItem();
19:
20: tempFaq.FaqCategory = new Lookup<byte>(reader.GetByte(SelectAllFaqAdapter.FaqCategoryIdColumnPosition));
21: tempFaq.Question = reader.GetString(SelectAllFaqAdapter.QuestionColumnPosition);
22: tempFaq.Answer = reader.GetString(SelectAllFaqAdapter.AnswerColumnPosition);
23: tempFaq.CreatedDate = reader.GetDateTime(SelectAllFaqAdapter.CreatedDateColumnPosition);
24: if (!reader.IsDBNull(SelectAllFaqAdapter.ModifiedDateColumnPosition)) {
25: tempFaq.ModifiedDate = reader.GetDateTime(SelectAllFaqAdapter.ModifiedDateColumnPosition);
26: }
27:
28: stateData.Add(tempFaq);
29: }
Notice that we use the column ordinal position constants defined by the adapter object to access values from the reader.