Megosztás a következőn keresztül:


SqlCommand.ExecuteReader Method

Definition

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:
 Microsoft::Data::SqlClient::SqlDataReader ^ ExecuteReader();
public Microsoft.Data.SqlClient.SqlDataReader ExecuteReader ();
override this.ExecuteReader : unit -> Microsoft.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.

// <Snippet1>
using System;
using System.Data;
using Microsoft.Data.SqlClient;



class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=SSPI";
        string qs = "SELECT OrderID, CustomerID FROM dbo.Orders;";
        CreateCommand(qs, str);
    }

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

            SqlCommand command = new SqlCommand(queryString, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}", reader[0]));
            }
        }
    }
    // </Snippet1>
}

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.

Applies to

ExecuteReader(CommandBehavior)

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

public:
 Microsoft::Data::SqlClient::SqlDataReader ^ ExecuteReader(System::Data::CommandBehavior behavior);
public Microsoft.Data.SqlClient.SqlDataReader ExecuteReader (System.Data.CommandBehavior behavior);
override this.ExecuteReader : System.Data.CommandBehavior -> Microsoft.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.

using System;
using System.Data;
using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=SSPI";
        string qs = "SELECT OrderID, CustomerID FROM dbo.Orders;";
        CreateCommand(qs, str);
    }

    private static void CreateCommand(string queryString,
        string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(
                   connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            connection.Open();
            SqlDataReader reader =
                command.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}", reader[0]));
            }
        }
    }

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.

Applies to