Using the ExecuteReader(RecordReadEventHandler) 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 stateless version of ExecuteReader. Here, we simply use a delegate method to print a few values from the SqlDataReader to the console.
1: public static void GetFaqByCategory(Lookup<byte> category) {
2: using (SqlConnection connection = new SqlConnection(ApplicationState.ConnectionString)) {
3: using (SelectFaqByCategoryIdAdapter adapter = new SelectFaqByCategoryIdAdapter(connection)) {
4: adapter.CategoryId = category.PrimaryKey.Value;
5: adapter.ExecuteReader(PrintRecord, CommandBehavior.CloseConnection);
6: }
7: }
8: }
9:
10: public static void PrintRecord(object sender, SqlDataReader reader){
11: Console.WriteLine(reader.GetString(SelectFaqByCategoryIdAdapter.QuestionColumnPosition));
12: Console.WriteLine(reader.GetString(SelectFaqByCategoryIdAdapter.AnswerColumnPosition));
13: }
Notice that we use the column ordinal position constants defined by the adapter object to access values from the reader.