共用方式為


SqlCeCommand.ExecuteReader 方法 (CommandBehavior)

藉由使用其中一個 CommandBehavior 值,將 CommandText 傳送到 Connection 並建立 SqlCeDataReader

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
Public Function ExecuteReader ( _
    behavior As CommandBehavior _
) As SqlCeDataReader
'用途
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
)
member ExecuteReader : 
        behavior:CommandBehavior -> SqlCeDataReader 
public function ExecuteReader(
    behavior : CommandBehavior
) : SqlCeDataReader

參數

傳回值

型別:System.Data.SqlServerCe.SqlCeDataReader
SqlCeDataReader 物件。

例外狀況

例外狀況 條件
InvalidOperationException

如果某項交易內容與原本連接登記的內容不同,便無法在那個交易內容中執行命令。

備註

SqlCeDataReader 支援可有效讀取大型二進位值的特殊模式。如需詳細資訊,請參閱 CommandBehavior 的 SequentialAccess 設定。

SqlCeDataReader 在使用中時,相關聯的 SqlCeConnection 會忙於服務 SqlCeDataReader。在這種狀態下,在呼叫 SqlCeDataReaderClose 方法之前,都只能在 SqlCeConnection 上執行 Close 作業。

範例

下列範例會建立 SqlCeCommand,然後透過傳遞 SQL SELECT 陳述式和 SqlCeConnection 物件來執行它。CommandBehavior 會設定為 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();
}

請參閱

參考

SqlCeCommand 類別

ExecuteReader 多載

System.Data.SqlServerCe 命名空間