SqlCommand.ExecuteReader 方法

定義

CommandText 傳送至 Connection,並建置 SqlDataReaderSends the CommandText to the Connection and builds a SqlDataReader.

多載

ExecuteReader()

CommandText 傳送至 Connection,並建置 SqlDataReaderSends the CommandText to the Connection and builds a SqlDataReader.

ExecuteReader(CommandBehavior)

CommandText 傳送至 Connection,並使用其中一個 CommandBehavior 值來建置 SqlDataReaderSends the CommandText to the Connection, and builds a SqlDataReader using one of the CommandBehavior values.

ExecuteReader()

CommandText 傳送至 Connection,並建置 SqlDataReaderSends the CommandText to the Connection and builds a SqlDataReader.

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

傳回

例外狀況

Value 設為 Stream 時,使用 BinaryVarBinary 以外的 SqlDbTypeA SqlDbType other than Binary or VarBinary was used when Value was set to Stream. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

-或--or-

Value 設為 TextReader 時,使用 CharNCharNVarCharVarCharXml 以外的 SqlDbTypeA SqlDbType other than Char, NChar, NVarChar, VarChar, or Xml was used when Value was set to TextReader.

-或--or-

Value 設定為 XmlReader 時,使用 Xml 以外的 SqlDbTypeA SqlDbType other than Xml was used when Value was set to XmlReader.

對鎖定的資料列執行命令時發生例外狀況。An exception occurred while executing the command against a locked row. 當您使用 Microsoft .NET Framework 1.0 版時不會產生這個例外狀況。This exception is not generated when you are using Microsoft .NET Framework version 1.0.

-或--or-

串流作業期間發生逾時。A timeout occurred during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

連接目前的狀態已關閉。The current state of the connection is closed. ExecuteReader() 需要開啟的 SqlConnectionExecuteReader() requires an open SqlConnection.

-或--or-

在串流作業期間已關閉或卸除的 SqlConnectionThe SqlConnection closed or dropped during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

StreamXmlReaderTextReader 物件在串流作業期間發生錯誤。An error occurred in a Stream, XmlReader or TextReader object during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

StreamXmlReaderTextReader 物件在串流作業期間已關閉。The Stream, XmlReader or TextReader object was closed during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

範例

下列範例會建立 SqlCommand,然後藉由傳遞一個做為 Transact-sql SELECT 語句的字串,以及用來連接到資料來源的字串來執行它。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);
        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()
        While reader.Read()
            Console.WriteLine("{0}", reader(0))
        End While
    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.

注意

如果交易已鎖死,則在呼叫 Read 之前,可能不會擲回例外狀況。If a transaction is deadlocked, an exception may not be thrown until Read is called.

Multiple Active Result Set (MARS)功能可讓您使用相同的連接來執行多個動作。The multiple active result set (MARS) feature allows for multiple actions using the same connection.

如果您使用 ExecuteReaderBeginExecuteReader 來存取 XML 資料,SQL Server 會傳回超過2033個字元的任何 XML 結果,其長度為每個2033個字元的多個資料列。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. 若要避免此行為,請使用 ExecuteXmlReaderBeginExecuteXmlReader 讀取 FOR XML 查詢。To avoid this behavior, use ExecuteXmlReader or BeginExecuteXmlReader to read FOR XML queries.

另請參閱

ExecuteReader(CommandBehavior)

CommandText 傳送至 Connection,並使用其中一個 CommandBehavior 值來建置 SqlDataReaderSends 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);
member this.ExecuteReader : System.Data.CommandBehavior -> System.Data.SqlClient.SqlDataReader
Public Function ExecuteReader (behavior As CommandBehavior) As SqlDataReader

參數

behavior
CommandBehavior

其中一個 CommandBehavior 值。One of the CommandBehavior values.

傳回

例外狀況

Value 設為 Stream 時,使用 BinaryVarBinary 以外的 SqlDbTypeA SqlDbType other than Binary or VarBinary was used when Value was set to Stream. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

-或--or-

Value 設為 TextReader 時,使用 CharNCharNVarCharVarCharXml 以外的 SqlDbTypeA SqlDbType other than Char, NChar, NVarChar, VarChar, or Xml was used when Value was set to TextReader.

-或--or-

Value 設定為 XmlReader 時,使用 Xml 以外的 SqlDbTypeA SqlDbType other than Xml was used when Value was set to XmlReader.

串流作業期間發生逾時。A timeout occurred during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

StreamXmlReaderTextReader 物件在串流作業期間發生錯誤。An error occurred in a Stream, XmlReader or TextReader object during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

在串流作業期間已關閉或卸除的 SqlConnectionThe SqlConnection closed or dropped during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

StreamXmlReaderTextReader 物件在串流作業期間已關閉。The Stream, XmlReader or TextReader object was closed during a streaming operation. 如需串流的詳細資訊,請參閱 SqlClient 串流支援For more information about streaming, see SqlClient Streaming Support.

範例

下列範例會建立 SqlCommand,然後藉由傳遞一個做為 Transact-sql SELECT 語句的字串,以及用來連接到資料來源的字串來執行它。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 設定為 CloseConnectionCommandBehavior 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();
        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)
        While reader.Read()
            Console.WriteLine("{0}", reader(0))
        End While
    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.

注意

使用 SequentialAccess 來取出大數值和二進位資料。Use SequentialAccess to retrieve large values and binary data. 否則,可能會發生 OutOfMemoryException,且連接將會關閉。Otherwise, an OutOfMemoryException might occur and the connection will be closed.

Multiple Active Result Set (MARS)功能可讓您使用相同的連接來執行多個動作。The multiple active result set (MARS) feature allows for multiple actions using the same connection.

如果您使用 ExecuteReaderBeginExecuteReader 來存取 XML 資料,SQL Server 會傳回超過2033個字元的任何 XML 結果,其長度為每個2033個字元的多個資料列。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. 若要避免此行為,請使用 ExecuteXmlReaderBeginExecuteXmlReader 讀取 FOR XML 查詢。To avoid this behavior, use ExecuteXmlReader or BeginExecuteXmlReader to read FOR XML queries.

另請參閱

適用於