SqlCeCommand.ExecuteReader Method

Sends the CommandText to the Connection and builds a SqlCeDataReader.

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

public SqlCeDataReader ExecuteReader()

ExceptionCondition
InvalidOperationException

Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.

While the SqlCeDataReader is in use, the associated SqlCeConnection is busy serving the SqlCeDataReader. In this state, you can create multiple readers in the same connection.

The following example creates a SqlCeCommand and then executes it by passing a string that is an SQL SELECT statement and a string for connecting to the data source.

SqlCeConnection conn = null;
SqlCeDataReader rdr = null;

try
{
    conn = new SqlCeConnection(connStr);
    conn.Open();

    SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM myTable", conn);
    cmd.Connection.Open();

    rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {
        Console.WriteLine(rdr.GetString(0));
    }
}
finally
{
    rdr.Close();
    conn.Close();
}


Show: