Data Access has been discontinued. Please refer to this page for more information.

How to: Translate to Primitive Types

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

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.
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( "-----------------------------------------" );
                   }
               }
           }
       }
   }
}
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

Optionally, if the execution of the command is expected to take long time, you can set an appropriate value for the CommandTimeout property of command. By default, it is the one specified in Runtime Configuration. If the command exceeds the timeout it will be terminated and SQLException will be thrown.