DOT NET Examples - DataReader

From Ingres Community Wiki

Jump to: navigation, search

Example .NET Data Provider Application

This simple example shows how to access a stream of Ingres data using the IngresDataReader class of the Ingres .NET Data Provider. The code opens a connection, executes a SQL query, writes the column headers, loops through the result set, writes the columns for each row in the result set, closes the IngresDataReader, and closes the connection (implicitly by the using(IngresConnection) construct).


The Example of .NET Source Code

You can cut and paste this into your favorite editor:

using System.Data;
using Ingres.Client;

static void ReaderDemo(string connstring)
{
    using (IngresConnection conn = new IngresConnection(connstring))
    {

        string strNumber;
        string strName;
        string strSSN;

        conn.Open();

        IngresCommand cmd = new IngresCommand(
            "select number, name, ssn  from personnel", conn);

        IngresDataReader reader = cmd.ExecuteReader();

        Console.Write(reader.GetName(0) + "\t");
        Console.Write(reader.GetName(1) + "\t");
        Console.Write(reader.GetName(2));
        Console.WriteLine();

        while (reader.Read())
        {
            strNumber= reader.IsDBNull(0)?
                "<none>":reader.GetInt32(0).ToString();
            strName  = reader.IsDBNull(1)?
                "<none>":reader.GetString(1);
            strSSN   = reader.IsDBNull(2)?
                "<none>":reader.GetString(2);

            Console.WriteLine(
                strNumber + "\t" + strName + "\t" + strSSN);
        }  // end while loop through result set

        reader.Close();
    }  // end using (IngresConnection)
}

Back to DOT NET Examples

Personal tools
Developing With