Previous Topic

Next Topic

How You Load the Airport Code and Name into the DataGridView Control

When the user selects a different region:

The IngresDataAdapter used for this query is defined directly in the source code and not created using the Forms Designer.

The IngresCommand and IngresDataAdapter are declared in the Ingres.cs source code file.

IngresCommand ingresSelectCommand4;

IngresDataAdapter ingresDataAdapter4;

The Ingres Data Adapater ingresDataAdapter4 is initialized in the InitializeIngres method.

private void InitializeIngres()

{

.

.

.

    ingresSelectCommand4 = new IngresCommand(

        "{ call get_my_airports( ccode = ?, area = ? ) }",

        ingresConnection1);

    ingresSelectCommand4.Connection = ingresConnection1;

    ingresDataAdapter4 = new IngresDataAdapter(ingresSelectCommand4);

.

    .

    .

    }

// The following query text shows the syntax for executing a database procedure.

ingresSelectCommand4 = new IngresCommand(

"{ call get_my_airports( ccode = ?, area = ? ) }",

ingresConnection1);

The database procedure, get_my_airports, is declared in the database, taking two parameters and returning rows that contain three columns:

These result columns are not returned with a relation header and cannot be referenced by name.

CREATE PROCEDURE get_my_airports(

    ccode   NCHAR(2) NOT NULL NOT DEFAULT,

    area NVARCHAR(30) NOT NULL NOT DEFAULT )

    RESULT ROW( CHAR(3) NOT NULL, NVARCHAR(30) NOT NULL,

                NVARCHAR(50) NOT NULL) AS

DECLARE

    iatacode NCHAR(3);

    place NVARCHAR(30);

    name NVARCHAR(50);

BEGIN

    FOR

        SELECT ap_iatacode, ap_place, ap_name

            INTO :iatacode, :place, :name

            FROM airport

            WHERE ap_ccode like :ccode AND ap_place like :area

            ORDER BY ap_iatacode

    DO

        RETURN ROW (:iatacode, :place, :name);

    ENDFOR;

END;

The IngresDataAdapter overcomes this by assigning column names composed of a fixed portion "result element" and a variable numerical portion, with an incrementing number beginning with zero, for example: result element0, result element1, and result element2.

Note: There is currently no mechanism for obtaining metadata for a database procedure from the database server, which prevents the IngresDataAdapter from generating an automatic typed DataSet. The result table for the row producing procedure must be defined using the DataSet designer.

Select the AirportDataSet.xsd in the Solution Explorer and open the DataSet Designer. Right-click the design panel and Add a DataTable.

AirportDataSet.xsd

Rename the newly created data table in the Properties panel. In this example, the table is called airportName.

Add two columns, called "result element0" and "result element2" and make "result element0" a key column. Set the DataType property of each column to System.String. (This should be the default.)

AirportName

Note: The database procedure returns three columns, but the table defines only two: IATA code and airport name.

The AirportDataSet now contains two definitions for tables: airport and airportName. These tables are identified by name in the AirportDataSet.Tables collection.

Airport DataSet

Note: Use an instance of the table defined in the AirportDataSet as a parameter to the ingresDataAdapter4.Fill method. For example:

AirportDataSet airportDataSet = new AirportDataSet();

LoadAirportNames(direction, comboBox.SelectedValue.ToString(),

airportDataSet.Tables["airportName"]);

Create a new binding source, airportNameBindingSource, and assign it to the dataGridView1.DataSource property.

Assign the data table to the airportNameBindingSource.DataSource property to bind the results to the dataGridView1 control.

BindingSource airportNameBindingSource = new BindingSource();

dataGridView1.DataSource = airportNameBindingSource;

airportNameBindingSource.DataSource =

airportDataSet.Tables["airportName"];

Call the method ingresDataAdapter4.Fill in the source code to populate the data table:

    public void LoadAirportName(int direction, String area,

        System.Data.DataTable airportNameTable)

    {

        IngresParameter country =

            new Ingres.Client.IngresParameter("ap_ccode",

                IngresType.NChar);

        IngresParameter region =

            new Ingres.Client.IngresParameter("ap_place",

                IngresType.NVarChar);

        region.Value = area;

        airportNameTable.Clear();

        ingresDataAdapter4.SelectCommand.Connection.Open();

        ingresDataAdapter4.SelectCommand.Prepare();

        ingresDataAdapter4.SelectCommand.Parameters.Clear();

        switch (direction)

        {

            case DEPART:

                country.Value = departing.country;

                break;

            case ARRIVE:

                country.Value = arriving.country;

                break;

        }

        ingresDataAdapter4.SelectCommand.Parameters.Add(country);

        ingresDataAdapter4.SelectCommand.Parameters.Add(region);

        ingresDataAdapter4.Fill(airportNameTable);

        ingresDataAdapter4.SelectCommand.Connection.Close();

    }


© 2006 Ingres Corporation. All rights reserved.