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.

    // Open a connection
    IfxConnection conn = 
        new IfxConnection(
        + "User ID=mylogin;password=mypassword"

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

    // Close the connection
    Console.ReadLine(); // Wait for a Return
catch(IfxException e) 
    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.