共用方式為


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)

Syntax

'Declaration
Public Function ExecuteReader As SqlCeDataReader
'Usage
Dim instance As SqlCeCommand
Dim returnValue As SqlCeDataReader

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

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

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.

Example

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.

Dim conn As SqlCeConnection = Nothing
Dim rdr As SqlCeDataReader = Nothing

Try
    conn = New SqlCeConnection(connStr)
    conn.Open()

    Dim cmd As New SqlCeCommand("SELECT * FROM myTable", conn)
    cmd.Connection.Open()

    rdr = cmd.ExecuteReader()

    While rdr.Read()
        Console.WriteLine(rdr.GetString(0))
    End While
Finally
    rdr.Close()
    conn.Close()
End Try
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();
}

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