15.8.4 Executing a search statement

This example displays all table data:

using System;
using System.Data;
using Hitachi.HiRDB;

namespace test_C
{
   class Sample
   {
       [STAThread]
       static void Main(string[] args)
       {
           try
           {
               // Create a Connection object
               HiRDBConnection cn = new HiRDBConnection("dsn=pc;");

               // Connect to the database
               cn.Open();

               // Create a Command object
               HiRDBCommand cm = new HiRDBCommand();
               cm.Connection = cn;
               cm.CommandText = "select a from ex";

               // Create a DataReader object
               HiRDBDataReader rd = cm.ExecuteReader();  ...............1
               int i;
               while(rd.Read())
               {
                   for (i = 0 ; i < rd.FieldCount ; i++)
                   {
                    Console.WriteLine(rd.GetName(i) + " - " +rd.GetValue(i));
                   }
               }  ......................................................2

               // Disconnect from the database
               cn.Close();
           }
           catch (HiRDBException ex)
           {
               Console.WriteLine(ex);
           }
           catch (System.Exception ex)
           {
               Console.WriteLine(ex);
           }
       }
   }
}

Explanation
  1. To execute a search, use the ExecuteReader method to create a HiRDBDataReader.
  2. Use the Read method to move on to the next row. Use the GetName method to acquire a column name, and use the GetValue method to acquire a column value.