Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
How to: Translate to Primitive Types
Programmer's Guide > Developer's Guide > Low Level (ADO) API > Translating DbDataReader > How to: Translate to Primitive Types

Glossary Item Box

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:

  1. Create and initialize a new OACommand object.
  2. Execute the command and obtain the DbDataReader object.
  3. 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()
    ' 1. Create a new instance of the OpenAccessContext.
    Using dbContext As New EntitiesModel()
        Using connection As IDbConnection = dbContext.Connection
            ' 2. Create a new command.
            Using command As IDbCommand = connection.CreateCommand()
                ' 3. Set the command text.
                command.CommandText = "Select Make From Cars"

                ' 4. Execute and translate the data reader.
                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