Retrieving data using SELECT

 Execute a SELECT statement

The SELECT statement allows you to retrieve information from the database. This section describes how to execute a SELECT statement and how to handle the result set it returns.

  1. Declare a ULCommand object, which holds the query.

    ULCommand cmd;
  2. Assign a statement to the object.

    cmd = conn.CreateCommand();
    cmd.Command = "SELECT MyColumn FROM MyTable";
  3. Execute the statement.

    Query results can be returned as one of several types of objects. In this example, a ULDataReader object is used. In the following code, the result of the SELECT statement contains a string, which is output to the command prompt.

    ULDataReader customerNames = prepStmt.ExecuteReader();
    int fc = customerNames.GetFieldCount();
    while( customerNames.MoveNext() ) {
      for ( int i = 0; i < fc; i++ ) {
        System.Console.Write(customerNames.GetString( i ) + " " );
      }
      System.Console.WriteLine();
    }