OleDbCommand.ExecuteReader 方法

定义

CommandText 发送到 Connection 并生成一个 OleDbDataReaderSends the CommandText to the Connection and builds an OleDbDataReader.

重载

ExecuteReader()

CommandText 发送到 Connection 并生成一个 OleDbDataReaderSends the CommandText to the Connection and builds an OleDbDataReader.

ExecuteReader(CommandBehavior)

CommandText 发送到 Connection,并使用 CommandBehavior 值之一构造 OleDbDataReaderSends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values.

ExecuteReader()

CommandText 发送到 Connection 并生成一个 OleDbDataReaderSends the CommandText to the Connection and builds an OleDbDataReader.

public:
 System::Data::OleDb::OleDbDataReader ^ ExecuteReader();
public System.Data.OleDb.OleDbDataReader ExecuteReader ();
override this.ExecuteReader : unit -> System.Data.OleDb.OleDbDataReader
Public Function ExecuteReader () As OleDbDataReader

返回

异常

不能在连接最初在其中登记的上下文之外的事务上下文中执行命令。Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.

示例

下面的示例创建一个 OleDbCommand,然后通过传递作为 SQL SELECT 语句的字符串以及用于连接到数据源的字符串来执行它。The following example creates an OleDbCommand, and then executes it by passing a string that is an SQL SELECT statement, and a string to use to connect to the data source.

public void CreateReader(string connectionString, string queryString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader[0].ToString());
        }
        reader.Close();
    }
}
Public Sub CreateReader(ByVal connectionString As String, _
    ByVal queryString As String)

    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(queryString, connection)

        connection.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            Console.WriteLine(reader(0).ToString())
        End While
        reader.Close()
    End Using
End Sub

注解

CommandType 属性设置为 StoredProcedure时,CommandText 属性应设置为存储过程的名称。When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. 调用 ExecuteReader时,该命令将执行此存储过程。The command executes this stored procedure when you call ExecuteReader.

在关闭 OleDbConnection之前,请先关闭 OleDbDataReader 对象。Before you close the OleDbConnection, first close the OleDbDataReader object. 如果打算重用 OleDbCommand 对象,还必须关闭 OleDbDataReader 对象。You must also close the OleDbDataReader object if you plan to reuse an OleDbCommand object.

另请参阅

ExecuteReader(CommandBehavior)

CommandText 发送到 Connection,并使用 CommandBehavior 值之一构造 OleDbDataReaderSends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values.

public:
 System::Data::OleDb::OleDbDataReader ^ ExecuteReader(System::Data::CommandBehavior behavior);
public System.Data.OleDb.OleDbDataReader ExecuteReader (System.Data.CommandBehavior behavior);
override this.ExecuteReader : System.Data.CommandBehavior -> System.Data.OleDb.OleDbDataReader
Public Function ExecuteReader (behavior As CommandBehavior) As OleDbDataReader

参数

behavior
CommandBehavior

CommandBehavior 值之一。One of the CommandBehavior values.

返回

异常

不能在连接最初在其中登记的上下文之外的事务上下文中执行命令。Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.

示例

下面的示例创建一个 OleDbCommand,然后通过传递一个字符串(一个 Transact-sql SELECT 语句)和一个用于连接到数据源的字符串来执行它。The following example creates an OleDbCommand, and then executes it by passing a string that is a Transact-SQL SELECT statement, and a string to use to connect to the data source. CommandBehavior 设置为 CloseConnectionCommandBehavior is set to CloseConnection.

public void CreateMyOleDbDataReader(string queryString,string connectionString) 
{
   OleDbConnection connection = new OleDbConnection(connectionString);
   OleDbCommand command = new OleDbCommand(queryString, connection);
   connection.Open();
   OleDbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
   while(reader.Read()) 
   {
      Console.WriteLine(reader.GetString(0));
   }
   reader.Close();
   //Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
}

Public Sub CreateMyOleDbDataReader(queryString As String, _
    connectionString As String)
    Dim connection As New OleDbConnection(connectionString)
    Dim command As New OleDbCommand(queryString, connection)
    connection.Open()
    Dim reader As OleDbDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
    While reader.Read()
        Console.WriteLine(reader.GetString(0))
    End While
    reader.Close()
    'Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
 End Sub

注解

当使用 OleDbCommand 对象的 ExecuteReader 方法指定 SingleRow 时,OLE DB 的 .NET Framework 数据提供程序将使用 OLE DB IRow接口(如果可用)执行绑定。When you specify SingleRow with the ExecuteReader method of the OleDbCommand object, the .NET Framework Data Provider for OLE DB performs binding using the OLE DB IRow interface if it is available. 否则,它将使用IRowset接口。Otherwise, it uses the IRowset interface. 如果期望 SQL 语句仅返回一行,则指定 SingleRow 也可提高应用程序性能。If your SQL statement is expected to return only a single row, specifying SingleRow can also improve application performance.

CommandType 属性设置为 StoredProcedure时,CommandText 属性应设置为存储过程的名称。When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. 调用 ExecuteReader时,该命令将执行此存储过程。The command executes this stored procedure when you call ExecuteReader.

OleDbDataReader 支持一种特殊模式,该模式允许有效读取较大的二进制值。The OleDbDataReader supports a special mode that enables large binary values to be read efficiently. 有关详细信息,请参阅 CommandBehaviorSequentialAccess 设置。For more information, see the SequentialAccess setting for CommandBehavior.

在关闭 OleDbConnection之前,请先关闭 OleDbDataReader 对象。Before you close the OleDbConnection, first close the OleDbDataReader object. 如果打算重用 OleDbCommand 对象,还必须关闭 OleDbDataReader 对象。You must also close the OleDbDataReader object if you plan to reuse an OleDbCommand object. 如果 OleDbDataReader 是在 CommandBehavior 设置为 CloseConnection时创建的,则关闭 OleDbDataReader 会自动关闭连接。If the OleDbDataReader is created with CommandBehavior set to CloseConnection, closing the OleDbDataReader closes the connection automatically.

另请参阅

适用于