|
The DataReader provides the fastest but least flexible way to retrieve data from the database. Data is returned as a read-only, forward-only stream of data that is returned one record at a time. If you need to retrieve many records rapidly, using a DataReader would require less memory than a DataSet, which would need to create a large table to hold the results.
The following code example, which requires the emp table (see Sample Tables for Pervasive.SQL ), shows how to execute a simple query on a Pervasive.SQL database and read the results using a DataReader:
// Open connection to Pervasive.SQL database PsqlConnection Conn; Conn = new PsqlConnection("ServerDSN=DEMODATA; UID=test; PWD=test; Server=localhost"); Conn.Open(); // Create a SQL command string strSQL = "SELECT ename FROM emp WHERE sal>50000"; PsqlCommand DBCmd = new PsqlCommand(strSQL, Conn); PsqlDataReader myDataReader; myDataReader = DBCmd.ExecuteReader(); while (myDataReader.Read()) { Console.WriteLine("High salaries: " + myDataReader["ename"].ToString()); } myDataReader.Close(); // Close the connection Conn.Close();
|
Chapter contents
Prev topic: Sample Tables Used in the Code Examples
|