Export (0) Print
Expand All

SqlCeCommand.ExecuteReader Method (CommandBehavior)

Sends the CommandText to the Connection and builds a SqlCeDataReader by using one of the CommandBehavior values.

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

public SqlCeDataReader ExecuteReader(
	CommandBehavior behavior
)

Parameters

behavior
Type: System.Data.CommandBehavior
One of the CommandBehavior values.

ExceptionCondition
InvalidOperationException

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

The SqlCeDataReader supports a special mode that enables large binary values to be read efficiently. For more information, see the SequentialAccess setting for CommandBehavior.

While the SqlCeDataReader is in use, the associated SqlCeConnection is busy serving the SqlCeDataReader. In this state, until you call the Close method of the SqlCeDataReader, you can perform only the Close operation on the SqlCeConnection.

The following example creates a SqlCeCommand, and then executes it by passing an SQL SELECT statement and a SqlCeConnection object. CommandBehavior is set to CloseConnection.

SqlCeConnection conn = new SqlCeConnection(connString);
SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM myTable", conn);

cmd.Connection.Open();
SqlCeDataReader rdr = null;

try
{
    // Execute the reader; make sure you alway close the 
    // reader after you're done using it (ideally in the finally block)
    //
    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    while (rdr.Read())
    {
        Console.WriteLine(rdr.GetString(0));
    }
}
finally
{
    // Closing the reader will also close the associated connection
    //
    rdr.Close();
}


Show:
© 2014 Microsoft