Compartilhar via


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)

Syntax

'Declaration
Public Function ExecuteReader ( _
    behavior As CommandBehavior _
) As SqlCeDataReader
'Usage
Dim instance As SqlCeCommand
Dim behavior As CommandBehavior
Dim returnValue As SqlCeDataReader

returnValue = instance.ExecuteReader(behavior)
public SqlCeDataReader ExecuteReader (
    CommandBehavior behavior
)
public:
SqlCeDataReader^ ExecuteReader (
    CommandBehavior behavior
)
public SqlCeDataReader ExecuteReader (
    CommandBehavior behavior
)
public function ExecuteReader (
    behavior : CommandBehavior
) : SqlCeDataReader
Not applicable.

Parameters

Return Value

A SqlCeDataReader object.

Exceptions

Exception type Condition

InvalidOperationException

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

Remarks

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.

Example

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.

Dim conn As New SqlCeConnection(connString)
Dim cmd As New SqlCeCommand("SELECT * FROM myTable", conn)

cmd.Connection.Open()
Dim rdr As SqlCeDataReader = Nothing

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))
    End While
Finally
    ' Closing the reader will also close the associated connection
    '
    rdr.Close()
End Try
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();
}

Platforms

Windows CE, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows XP Professional x64 Edition, Windows XP SP2

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0

.NET Compact Framework

Supported in: 2.0, 1.0

See Also

Reference

SqlCeCommand Class
SqlCeCommand Members
System.Data.SqlServerCe Namespace