Retrieving database records v9.0.3.1

You can use a SELECT statement to retrieve records from the database using a SELECT command. To execute a SELECT statement you must:

  1. Create and open a database connection.
  2. Create an EDBCommand object that represents the SELECT statement.
  3. Execute the command with the ExecuteReader() method of the EDBCommand object returning EDBDataReader.
  4. Loop through the EDBDataReader, displaying the results or binding the EDBDataReader to some control.

An EDBDataReader object represents a forward-only and read-only stream of database records, presented one record at a time. To view a subsequent record in the stream, you must call the Read() method of the EDBDataReader object.

The example that follows:

  1. Imports the EDB Postgres Advanced Server namespace EnterpriseDB.EDBClient.
  2. Initializes an EDBCommand object with a SELECT statement.
  3. Opens a connection to the database.
  4. Executes the EDBCommand by calling the ExecuteReader method of the EDBCommand object.

The results of the SQL statement are retrieved into an EDBDataReader object.

Loop through the contents of the EDBDataReader object to display the records returned by the query in a WHILE loop.

The Read() method advances to the next record (if there is one) and returns true if a record exists. It returns false if EDBDataReader has reached the end of the result set.

This program should output the following text on the console :

Department Number: 10   Department Name: ACCOUNTING     Department Location: NEW YORK
Department Number: 20   Department Name: RESEARCH       Department Location: DALLAS
Department Number: 30   Department Name: SALES          Department Location: CHICAGO
Department Number: 40   Department Name: OPERATIONS     Department Location: BOSTON

Retrieving a single database record

To retrieve a single result from a query, use the ExecuteScalar() method of the EDBCommand object. The ExecuteScalar() method returns the first value of the first column of the first row of the result set generated by the specified query.

This program should output the following text on the console :

Max Salary: 5000.00

The sample includes an explicit conversion of the value returned by the ExecuteScalar() method. The ExecuteScalar() method returns an object (it’s a decimal value boxed into an object). You can access the native value by using an explicit cast to a nullable decimal value.