SqlCeDataReader.Read Method

Advances SqlCeDataReader to the next record.

Namespace:  System.Data.SqlServerCe
Assembly:  System.Data.SqlServerCe (in System.Data.SqlServerCe.dll)

public override bool Read()

Return Value

Type: System.Boolean
true if there are more rows; otherwise, false.



The default position of the SqlCeDataReader is prior to the first record. To begin accessing any data, you must call Read. The default position of the SqlCeResultSet is the first record. Calling SqlCeResultSet.Read method immediately after creating the SqlCeResultSet will move the cursor to the second record. For SqlCeDataReader.Read, the behaviour is different from that of SqlCeResultSet.Read.

While SqlCeDataReader is in use, the associated SqlCeConnection is busy serving it until you call Close.

The following example creates a SqlCeConnection, a SqlCeCommand, and a SqlCeDataReader. The example reads through the data and writes it out to the console. Finally, the example closes the SqlCeDataReader, then the SqlCeConnection.

SqlCeConnection conn = null;
SqlCeCommand cmd = null;
SqlCeDataReader rdr = null;

    // Open the connection and create a SQL command
    conn = new SqlCeConnection("Data Source = AdventureWorks.sdf");

    cmd = new SqlCeCommand("SELECT * FROM DimEmployee", conn);

    rdr = cmd.ExecuteReader();

    // Iterate through the results
    while (rdr.Read())
        int employeeID = rdr.GetInt32(0);   // or: rdr["EmployeeKey"];
        string lastName = rdr.GetString(5); // or: rdr["FirstName"];

    // Always dispose data readers and commands as soon as practicable
    // Close the connection when no longer needed