This topic demonstrates how to translate a DbDataReader object to a collection of primitive types.
 |
To run the samples in this topic, you need to use\import the System.Data, System.Data.Common and Telerik.OpenAccess.Data.Common namespaces. |
Basically, the steps you need to perform are:
- Create and initialize a new OACommand object.
- Execute the command and obtain the DbDataReader object.
- With the reader in hand, use the generic Translate<T> method of the context.
| C# |
Copy Code |
|
private static void TranslateDbDataReaderToPrimitiveTypes() { // 1. Create a new instance of the OpenAccessContext. using ( EntitiesModel dbContext = new EntitiesModel() ) { using ( IDbConnection connection = dbContext.Connection ) { // 2. Create a new command. using ( IDbCommand command = connection.CreateCommand() ) { // 3. Set the command text. command.CommandText = "Select Make From Cars";
// 4. Execute and translate the data reader. using ( IDataReader reader = command.ExecuteReader() ) { IEnumerable<string> result = dbContext.Translate<string>( reader as DbDataReader ); foreach ( string name in result ) { Console.WriteLine( "Car Name: {0}", name ); Console.WriteLine( "-----------------------------------------" ); } } } } } } |
| VB.NET |
Copy Code |
|
Private Sub TranslateDbDataReaderToPrimitiveTypes() Using dbContext As New EntitiesModel() Using connection As IDbConnection = dbContext.Connection Using command As IDbCommand = connection.CreateCommand() command.CommandText = "Select Make From Cars"
Using reader As IDataReader = command.ExecuteReader() Dim result As IEnumerable(Of String) = dbContext.Translate(Of String)(TryCast(reader, DbDataReader)) For Each name As String In result Console.WriteLine("Car Name: {0}", name) Console.WriteLine("-----------------------------------------") Next name End Using End Using End Using End Using End Sub |