Uredi

Deli z drugimi prek


SqlCommand.ExecuteReader Method

Definition

Sends the CommandText to the Connection and builds a SqlDataReader.

Overloads

ExecuteReader()

Sends the CommandText to the Connection and builds a SqlDataReader.

ExecuteReader(CommandBehavior)

Sends the CommandText to the Connection, and builds a SqlDataReader using one of the CommandBehavior values.

ExecuteReader()

Sends the CommandText to the Connection and builds a SqlDataReader.

public:
 System::Data::SqlClient::SqlDataReader ^ ExecuteReader();
public System.Data.SqlClient.SqlDataReader ExecuteReader ();
override this.ExecuteReader : unit -> System.Data.SqlClient.SqlDataReader
member this.ExecuteReader : unit -> System.Data.SqlClient.SqlDataReader
Public Function ExecuteReader () As SqlDataReader

Returns

A SqlDataReader object.

Exceptions

A SqlDbType other than Binary or VarBinary was used when Value was set to Stream. For more information about streaming, see SqlClient Streaming Support.

-or-

A SqlDbType other than Char, NChar, NVarChar, VarChar, or Xml was used when Value was set to TextReader.

-or-

A SqlDbType other than Xml was used when Value was set to XmlReader.

An exception occurred while executing the command against a locked row. This exception is not generated when you are using Microsoft .NET Framework version 1.0.

-or-

A timeout occurred during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

The current state of the connection is closed. ExecuteReader() requires an open SqlConnection.

-or-

The SqlConnection closed or dropped during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

An error occurred in a Stream, XmlReader or TextReader object during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

The Stream, XmlReader or TextReader object was closed during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

Examples

The following example creates a SqlCommand, 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.

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        using(SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}", reader[0]));
            }
        }
    }
}
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()

        Dim command As New SqlCommand(queryString, connection)
        Dim reader As SqlDataReader = command.ExecuteReader()
        Try
            While reader.Read()
                Console.WriteLine("{0}", reader(0))
            End While
        Finally
            ' Always call Close when done reading.
            reader.Close()
        End Try
    End Using
End Sub

Remarks

When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. The command executes this stored procedure when you call ExecuteReader.

Note

If a transaction is deadlocked, an exception may not be thrown until Read is called.

The multiple active result set (MARS) feature allows for multiple actions using the same connection.

If you use ExecuteReader or BeginExecuteReader to access XML data, SQL Server will return any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use ExecuteXmlReader or BeginExecuteXmlReader to read FOR XML queries.

See also

Applies to

ExecuteReader(CommandBehavior)

Sends the CommandText to the Connection, and builds a SqlDataReader using one of the CommandBehavior values.

public:
 System::Data::SqlClient::SqlDataReader ^ ExecuteReader(System::Data::CommandBehavior behavior);
public System.Data.SqlClient.SqlDataReader ExecuteReader (System.Data.CommandBehavior behavior);
override this.ExecuteReader : System.Data.CommandBehavior -> System.Data.SqlClient.SqlDataReader
member this.ExecuteReader : System.Data.CommandBehavior -> System.Data.SqlClient.SqlDataReader
Public Function ExecuteReader (behavior As CommandBehavior) As SqlDataReader

Parameters

behavior
CommandBehavior

One of the CommandBehavior values.

Returns

A SqlDataReader object.

Exceptions

A SqlDbType other than Binary or VarBinary was used when Value was set to Stream. For more information about streaming, see SqlClient Streaming Support.

-or-

A SqlDbType other than Char, NChar, NVarChar, VarChar, or Xml was used when Value was set to TextReader.

-or-

A SqlDbType other than Xml was used when Value was set to XmlReader.

A timeout occurred during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

An error occurred in a Stream, XmlReader or TextReader object during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

The SqlConnection closed or dropped during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

The Stream, XmlReader or TextReader object was closed during a streaming operation. For more information about streaming, see SqlClient Streaming Support.

Examples

The following example creates a SqlCommand, 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 is set to CloseConnection.

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        connection.Open();
        using(SqlDataReader reader =
            command.ExecuteReader(CommandBehavior.CloseConnection))
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}", reader[0]));
            }
        }
    }
}
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()
        Dim reader As SqlDataReader = _
            command.ExecuteReader(CommandBehavior.CloseConnection)
        Try
            While reader.Read()
                Console.WriteLine("{0}", reader(0))
            End While
        Finally
            ' Always call Close when done reading.
            reader.Close()
        End Try
    End Using
End Sub

Remarks

When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. The command executes this stored procedure when you call ExecuteReader.

Note

Use SequentialAccess to retrieve large values and binary data. Otherwise, an OutOfMemoryException might occur and the connection will be closed.

The multiple active result set (MARS) feature allows for multiple actions using the same connection.

If you use ExecuteReader or BeginExecuteReader to access XML data, SQL Server will return any XML results greater than 2,033 characters in length in multiple rows of 2,033 characters each. To avoid this behavior, use ExecuteXmlReader or BeginExecuteXmlReader to read FOR XML queries.

See also

Applies to