IBM® Informix® Client Software Development Kit, Version 4.10

Retrieve a single value

You can use the IfxCommand.ExecuteScalar method when you know that the SQL you want to execute will return a single value.

The IfxCommand.ExecuteScalar method returns a System.Object. You must cast this to the type of data that you expect to be returned. This example returns the output of COUNT(*) which is a decimal value so the System.Object is cast to type System.Decimal.

For more information about the IfxCommand class, see IfxCommand class.

try 
{
    // Open a connection
    IfxConnection conn = 
        new IfxConnection(
        "Host=myhost;Service=1541;Server=myifxserver;Database=stores_demo;"
        + "User ID=mylogin;password=mypassword"
        );
    conn.Open();

    // Create an SQL command
    IfxCommand cmd = new IfxCommand(
        "SELECT COUNT(*) FROM customer", 
        conn
        );
    Decimal ccount = (Decimal)cmd.ExecuteScalar();
        Console.WriteLine("There are " + ccount + " customers");

    // Close the connection
    conn.Close();
    Console.ReadLine(); // Wait for a Return
}
catch(IfxException e) 
{
    Console.WriteLine(e.ToString());
    Console.ReadLine(); //Wait for a Return
}


Examples exchange | Troubleshooting

To find the PDF, see Publications for the IBM Informix 12.10 family of products.
For the release notes, documentation notes, and/or machine notes, see the Release Notes page.