Nonstop Database, HiRDB Version 9 UAP Development Guide

[Contents][Index][Back][Next]

16.11.7 Checking for an error in SQL statements and acquiring error information

This example checks for an error in the SQL statements and acquires error information.

Although the sample program is coded in Visual C# .NET, the program in Visual Basic.NET would be almost the same (if necessary, change information as appropriate).

 
using System;
using System.IO;
using System.Data;
using System.Windows.Forms;
using Hitachi.HiRDB;                // .NET Framework Data Provider for HiRDB
 
namespace SAMPLE
{
    public class SAMPLE
    {
        static void Main()
        {
 
            HiRDBConnection connection1 = new HiRDBConnection
            ("datasource=C:\\Windows\\HiRDB.ini;UID=USER1;PWD=USER1;");
            HiRDBCommand cm = new HiRDBCommand();
 
                try
                {
                    // Connect  ........................................1
                    connection1.Open();
                    cm.Connection = connection1;
 
                    // *********************************************
                    // Example of retrieval by SAMPLE1 (C1 INT, C2 INT, C3 VARCHAR(30))  ...2
                    // *********************************************
                    // Create a parameter object
                    HiRDBParameter    par = cm.CreateParameter();
                    // Set parameter attributes
                    // Input parameter
                    par.Direction = ParameterDirection.Input;
                    // INTEGER type
                    par.HiRDBType = HiRDBType.Integer;
                    int aValue;
                    aValue = 200;
                    // Set parameter value
                    par.Value = aValue;
 
                    // Set SQL statement
                    cm.CommandText = "SELECT C2,C3 FROM SAMPLE1 WHERE C1=?
                                      WITH EXCLUSIVE LOCK NO WAIT";
                    // Assign parameter object
                    cm.Parameters.Add(par);
                    // Acquire DataReader object
                    HiRDBDataReader dr = cm.ExecuteReader();
                    int cnt=1;
                    Console.WriteLine("**** Searching ****");
                    while(dr.Read())
                    {
                        Console.WriteLine("**** "+cnt+"Searching row ***");
                        // Display data in C2
                        Console.WriteLine("C2="+dr.GetInt32(0));
                        // Display data in C3
                        Console.WriteLine("C3="+dr.GetString(1));
                        cnt ++;
                    }
                    // Free DataReader
                    dr.Close();
                    // Disconnect  .......................................3
                    connection1.Close();
                    connection1.Dispose();
 
                }
                catch (HiRDBException ex)  ..............................4
                {
                    // Output error information
                    Console.WriteLine(ex);
                    // Acquire individual information by the following process
                    // Output SQLCODE
                    Console.WriteLine("SQLCODE="+ex.ErrorCode);
                    // Output SQLERRM (SQL message)
                    Console.WriteLine("SQLERRM=" + ex.Message);
                }
        }
    }
}

Explanation:
  1. Uses the Open method to connect to HiRDB.
  2. Executes the SQL statement that displays a row that satisfies a specified condition.
  3. Uses the Close method to disconnect from HiRDB.
  4. Returns SQLException and outputs error information in the event of an error.