Retrieving database records v18.104.22.168
You can use a
SELECT statement to retrieve records from the database using a
SELECT command. To execute a
SELECT statement you must:
- Create and open a database connection.
- Create an
EDBCommandobject that represents the
- Execute the command with the
ExecuteReader()method of the
- Loop through the
EDBDataReader, displaying the results or binding the
EDBDataReaderto some control.
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
The example that follows:
- Imports the EDB Postgres Advanced Server namespace
- Initializes an
EDBCommandobject with a
- Opens a connection to the database.
- Executes the
EDBCommandby calling the
ExecuteReadermethod of the
The results of the SQL statement are retrieved into an
Loop through the contents of the
EDBDataReader object to display the records returned by the query in a
Read() method advances to the next record (if a record exists) and returns
true if a record exists. It returns
EDBDataReader has reached the end of the result set.
To exercise the sample code, save the code in your default web root directory in a file named
selectEmployees.aspx. Then, to invoke the program, enter the following URL into a browser:
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
DataSet generated by the specified query.
Save the sample code in a file named
selectscalar.aspx in a web root directory.
To invoke the sample code, enter the following in a browser:
The sample includes an explicit conversion of the value returned by the
ExecuteScalar() method. The
ExecuteScalar() method returns an object. To view the object, you must convert it to an integer value by using the
- On this page
- Retrieving a single database record