RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TAdoDbxDataReader Class

Class to read rows forward-only from a data source.

Pascal
TAdoDbxDataReader = class(DbDataReader);
C++
class TAdoDbxDataReader : public DbDataReader;

Borland.Data.AdoDbxClientProvider

A TAdoDbxDataReader is returned as a result of a SELECT or stored procedure execution from a call to TAdoDbxCommand.ExecuteReader. Because there is no public constructor, you cannot directly instantiate a TAdoDbxDataReader. Instead, obtain the TAdoDbxDataReader through the ExecuteReader method of the TAdoDbxCommand object.  

The TAdoDbxDataReader class is an implementation of the ADO.NET DbDataReader class. 

The TAdoDbxDataReader provides a forward-only reader and the associated metadata. TAdoDbxDataReader methods such as GetName, GetDataTypeName, GetFieldType and GetDataTypeName provide the metadata.  

For all of these methods, you must pass the ordinal of the column, which is zero-based, in the ordinal parameter. Given a column name, GetOrdinal returns the column ordinal or position in the select list. GetName, GetDataTypeName and GetFieldType return the name, SQL datatype name, and the .NET Framework System.Type, respectively, for a particular column. GetSchemaTable also can be used to retrieve the metadata of a column as a DataTable.  

You can call TAdoDbxDataReader.Read to fetch records one after the other until a false value is returned, which indicates that you have hit the EOF. Before accessing individual column values, you can check to see if the data is NULL by calling IsDBNull. Then, depending on the datatype, you can call one of the field accessor methods, such as GetInt16, GetInt32 or GetFloat

You can access BLOB data as a byte array or a character array by calling GetBytes or GetChars. A null buffer passed to these methods returns the size of the BLOB data available. The current implementation of TAdoDbxDataReader does not support fetching BLOB data by specifying offsets. 

The initial state of the TAdoDbxDataReader returned from a call to TAdoDbxCommand.ExecuteReader is open. Once all of the records have been fetched, you can call the Close method to free all of the column-related resources. To find out if a TAdoDbxDataReader is closed, you can check the IsClosed property. If the TAdoDbxDataReader is closed, it returns True, otherwise False. If the CommandBehavior parameter is CloseConnection in ExecuteReader, the TAdoDbxConnection used for executing the SQL is also closed when the TAdoDbxDataReader is closed. The NextResult method returns True if more results are available from a stored procedure execution.  

The following C# code shows how to retrieve data using a TAdoDbxDataReader, assuming that a you already have a valid connection and command.

 public static void ExecuteCommand ( TAdoDbxCommand Comm ) 
   {
     Comm.CommandText  =  " SELECT * FROM EMPLOYEE";
      Comm.Prepare();      
      TAdoDbxDataReader Reader = Comm.ExecuteReader();
      if ( Reader != null )
      {
         while (Reader.Read())
         {
            ShowData(Reader);                          
         }
         Reader.Close();
      }
      Comm.Close();
   }

   public static void ShowData(TAdoDbxDataReader Reader)
   {
      long retVal = 0, startIndex = 0;
      int  buffSize = 0;
      char []buffer = null;

      for (Int32 index = 0; index < Reader.FieldCount; index++)
      {
         //Check for NULL
         if ( Reader.IsDBNull(index) )
         {
            Console.Write("NULL");
         }
         else
         {
            Type t =  Reader.GetFieldType(index);
            if (t == typeof(Int16) )
               Console.Write(Reader.GetInt16(index));
            else if ( t == typeof(Int32) )
               Console.Write(Reader.GetInt32(index));
            else if ( t == typeof(String) )
               Console.Write(Reader.GetString(index));
            else if ( t == typeof(float) )
               Console.Write(Reader.GetFloat(index));
            else if ( t == typeof(double) )
               Console.Write(Reader.GetDouble(index));
            else if ( t == typeof(DateTime) )
               Console.Write(Reader.GetDateTime(index));
            else if ( t == typeof(Decimal) )
               Console.Write(Reader.GetDecimal(index));
            else if ( (t == typeof(Byte[])) || (t == typeof(Char[])) )
            {
               String DataType =  Reader.GetDataTypeName(index);
               if (DataType == "Blob")
               {
                  retVal = Reader.GetChars(index, 0, null, 0, 0);
                  Console.Write("Blob Size = " + retVal);
                  buffSize = (int) retVal;

                  //Display only character blob data
                  if ( retVal > 0 && (t== typeof(Char[])) )
                  {
                     buffer = new char[buffSize];
                     startIndex = 0;
                     retVal = Reader.GetChars(index, startIndex, buffer, 0, buffSize);
                     for (int i = 0; i < buffer.Length; i++ )
                        Console.Write(buffer[i]);
                 }
                    
              }
                 
            }

         }

         if (index < Reader.FieldCount -1)
           Console.Write(", ");
      }

      Console.WriteLine();           

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!