SqlCeCommand.ExecuteReader 方法 (CommandBehavior)

CommandText 发送到 Connection,并使用某一 CommandBehavior 值生成 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 命名空间