SqlCommand::ExecuteReader Method (CommandBehavior)
Sends the CommandText to the Connection, and builds a SqlDataReader using one of the CommandBehavior values.
Assembly: System.Data (in System.Data.dll)
Parameters
- behavior
-
Type:
System.Data::CommandBehavior
One of the CommandBehavior values.
| Exception | Condition |
|---|---|
| InvalidCastException | A SqlDbType other than Binary or VarBinary was used when Value was set to Stream. For more information about streaming, see SqlClient Streaming Support. A SqlDbType other than Char, NChar, NVarChar, VarChar, or Xml was used when Value was set to TextReader. A SqlDbType other than Xml was used when Value was set to XmlReader. |
| SqlException | A timeout occurred during a streaming operation. For more information about streaming, see SqlClient Streaming Support. |
| IOException | An error occurred in a Stream, XmlReader or TextReader object during a streaming operation. For more information about streaming, see SqlClient Streaming Support. |
| InvalidOperationException | The SqlConnection closed or dropped during a streaming operation. For more information about streaming, see SqlClient Streaming Support. |
| ObjectDisposedException | The Stream, XmlReader or TextReader object was closed during a streaming operation. For more information about streaming, see SqlClient Streaming Support. |
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. For more information, see article Q310378, "PRB: XML Data Is Truncated When You Use SqlDataReader," in the Microsoft Knowledge Base at http://support.microsoft.com.
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.
Available since 1.1
