How to: Translate to Primitive Types
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.
private static void TranslateDbDataReaderToPrimitiveTypes()
{
// 1. Create a new instance of the OpenAccessContext.
using ( FluentModel dbContext = new FluentModel() )
{
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 FluentModel()
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.