OleDbCommand.ExecuteReader Método
Definição
Envia o CommandText para o Connection e cria um OleDbDataReader.Sends the CommandText to the Connection and builds an OleDbDataReader.
Sobrecargas
| ExecuteReader() |
Envia o CommandText para o Connection e cria um OleDbDataReader.Sends the CommandText to the Connection and builds an OleDbDataReader. |
| ExecuteReader(CommandBehavior) |
Envia o CommandText para o Connection e cria um OleDbDataReader usando um dos valores CommandBehavior.Sends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values. |
ExecuteReader()
Envia o CommandText para o Connection e cria um OleDbDataReader.Sends 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
member this.ExecuteReader : unit -> System.Data.OleDb.OleDbDataReader
Public Function ExecuteReader () As OleDbDataReader
Retornos
Um objeto OleDbDataReader.An OleDbDataReader object.
Exceções
Não é possível executar um comando em um contexto de transação diferente do contexto no qual a conexão foi inscrita originalmente.Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.
Exemplos
O exemplo a seguir cria um OleDbCommand e o executa passando uma cadeia de caracteres que é uma instrução SQL SELECT e uma cadeia de caracteres a ser usada para se conectar à fonte de dados.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
Comentários
Quando a CommandType propriedade é definida como StoredProcedure , a CommandText propriedade deve ser definida como o nome do procedimento armazenado.When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. O comando executa esse procedimento armazenado quando você chama ExecuteReader .The command executes this stored procedure when you call ExecuteReader.
Antes de fechar o OleDbConnection , primeiro feche o OleDbDataReader objeto.Before you close the OleDbConnection, first close the OleDbDataReader object. Você também deve fechar o OleDbDataReader objeto se planeja reutilizar um OleDbCommand objeto.You must also close the OleDbDataReader object if you plan to reuse an OleDbCommand object.
Aplica-se a
ExecuteReader(CommandBehavior)
Envia o CommandText para o Connection e cria um OleDbDataReader usando um dos valores CommandBehavior.Sends 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
member this.ExecuteReader : System.Data.CommandBehavior -> System.Data.OleDb.OleDbDataReader
Public Function ExecuteReader (behavior As CommandBehavior) As OleDbDataReader
Parâmetros
- behavior
- CommandBehavior
Um dos valores de CommandBehavior.One of the CommandBehavior values.
Retornos
Um objeto OleDbDataReader.An OleDbDataReader object.
Exceções
Não é possível executar um comando em um contexto de transação diferente do contexto no qual a conexão foi inscrita originalmente.Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.
Exemplos
O exemplo a seguir cria um OleDbCommand e, em seguida, executa-o passando uma cadeia de caracteres que é uma instrução SELECT do Transact-SQL e uma cadeia de caracteres a ser usada para se conectar à fonte de dados.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 é definido como CloseConnection.CommandBehavior 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
Comentários
Quando você especifica SingleRow com o ExecuteReader método do OleDbCommand objeto, o .NET Framework provedor de dados para OLE DB executa a Associação usando a interface OLE DB IRow se ela estiver disponível.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. Caso contrário, ele usará a interface IRowset .Otherwise, it uses the IRowset interface. Se a instrução SQL dever retornar apenas uma única linha, especificar SingleRow também poderá melhorar o desempenho do aplicativo.If your SQL statement is expected to return only a single row, specifying SingleRow can also improve application performance.
Quando a CommandType propriedade é definida como StoredProcedure , a CommandText propriedade deve ser definida como o nome do procedimento armazenado.When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. O comando executa esse procedimento armazenado quando você chama ExecuteReader .The command executes this stored procedure when you call ExecuteReader.
O OleDbDataReader oferece suporte a um modo especial que permite que valores binários grandes sejam lidos com eficiência.The OleDbDataReader supports a special mode that enables large binary values to be read efficiently. Para obter mais informações, consulte a SequentialAccess configuração para CommandBehavior .For more information, see the SequentialAccess setting for CommandBehavior.
Antes de fechar o OleDbConnection , primeiro feche o OleDbDataReader objeto.Before you close the OleDbConnection, first close the OleDbDataReader object. Você também deve fechar o OleDbDataReader objeto se planeja reutilizar um OleDbCommand objeto.You must also close the OleDbDataReader object if you plan to reuse an OleDbCommand object. Se o OleDbDataReader for criado com CommandBehavior definido como CloseConnection , fechar o fechará OleDbDataReader a conexão automaticamente.If the OleDbDataReader is created with CommandBehavior set to CloseConnection, closing the OleDbDataReader closes the connection automatically.