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
)
public SqlCeDataReader ExecuteReader (
    CommandBehavior behavior
)
public function ExecuteReader (
    behavior : CommandBehavior
) : SqlCeDataReader
不适用。

参数

返回值

一个 SqlCeDataReader 对象。

异常

异常类型 条件

InvalidOperationException

不能在连接最初在其中登记的上下文之外的事务上下文中执行命令。

备注

SqlCeDataReader 支持一种特殊的模式,在此模式下可以有效地读取较大的二进制值。有关更多信息,请参见 CommandBehaviorSequentialAccess 设置。

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();
}

平台

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。

版本信息

.NET Framework

受以下版本支持:3.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

SqlCeCommand 类
SqlCeCommand 成员
System.Data.SqlServerCe 命名空间