Sqlcommand. executereader Method

Source: Internet
Author: User
Tags truncated
Sqlcommand. executereader Method

Set commandtext
Send to connection
And generate a sqldatareader.

Reload list

Name Description
Sqlcommand. executereader
()
Send commandtext to connection and generate a sqldatareader.

Supported by. NET Compact framework.

Sqlcommand. executereader
(Commandbehavior)
Send commandtext to connection and use commandbehavior
Generate a sqldatareader.

Supported by. NET Compact framework.

Sqlcommand. executereader method ()

Set commandtext
Send to connection
And generate a sqldatareader.

Namespace:System. Data. sqlclient
Assembly:System. Data (in system. Data. dll
Medium)

Syntax

C # 
public SqlDataReader ExecuteReader ()
C ++
public:SqlDataReader^ ExecuteReader ()
J #
public SqlDataReader ExecuteReader ()
JScript
public function ExecuteReader () : SqlDataReader

Return Value

OneSqldatareaderObject. Exception

Exception type Condition

Exception

Failed to execute this command.

Sqlexception

An exception occurred while executing the command on the locked line. If Microsoft. NET Framework 1.0 is used, this exception is not generated.

Remarks

When commandtype
Set propertyStoredprocedure,CommandtextAttribute should be set to the name of the stored procedure. When
ExecutereaderThe command will execute this stored procedure.

If you use SQL Server versions earlier than SQL Server 2005SqldatareaderThe sqlconnection
Will be busySqldatareaderService. When you are in this statusSqlconnection
. Unless you callSqldatareaderClose
Method, otherwise it will remain in this status. From SQL Server 2005, the multi-activity result set (MARS) function allows multiple operations to use the same connection.

If you useExecutereaderOr beginexecutereader
When accessing XML data, SQL server returns all XML data with a length greater than 2,033 characters in multiple lines (2,033 characters in each line ).
Result. To avoid this behavior, use executexmlreader
Or beginexecutexmlreader
Read the for XML query. For more information, see located in http://support.microsoft.com/default.aspx? Ln = ZH-CN.
Article q310378 "PRB: XML data is truncated when you use
Sqldatareader "(PRB: XML data is truncated when sqldatareader is used ).

Example

The following example creates a sqlcommand and then executes it by passing two strings, one of which is
The SELECT statement. The other string is used to connect to the data source.

C # 

Copy code
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]));}} 

}

Sqlcommand. executereader method (commandbehavior)

Set commandtext
Send to connection and use
CommandbehaviorGenerate a sqldatareader.

Namespace:System. Data. sqlclient
Assembly:System. Data (in system. Data. dll
Medium)

Syntax

C #
public SqlDataReader ExecuteReader (CommandBehavior behavior)
Parameters 
Behavior

Commandbehavior
Value.

Return Value

OneSqldatareaderObject. Remarks

When commandtype
Set propertyStoredprocedure,CommandtextAttribute should be set to the name of the stored procedure. When executereader is called
The command will execute this stored procedure.

SqldatareaderSupports a special mode in which large binary values can be effectively read. For more information, see
CommandbehaviorOfSequentialaccess.

If you use SQL Server versions earlier than SQL Server 2005SqldatareaderThe sqlconnection
Will be busySqldatareaderService. When you are in this statusSqlconnection
. Call sqldatareader. Close
This has been the case before. IfSqldatareaderAnd setCommandbehaviorSet
Closeconnection, DisableSqldatareaderThe connection is automatically closed. From SQL Server 2005
The multi-activity result set (MARS) function allows multiple operations to use the same connection.

If you useExecutereaderOr beginexecutereader
When accessing XML data, SQL server returns all XML data with a length greater than 2,033 characters in multiple lines (2,033 characters in each line ).
Result. To avoid this behavior, use executexmlreader
Or beginexecutexmlreader
Read the for XML query. For more information, see located in http://support.microsoft.com/default.aspx? Ln = ZH-CN.
Article q310378 "PRB: XML data is truncated when you use
Sqldatareader "(PRB: XML data is truncated when sqldatareader is used ).

Example

The following example creates a sqlcommand and then executes it by passing two strings, one of which is
The SELECT statement. The other string is used to connect to the data source.CommandbehaviorSet to closeconnection.

C # 

Copy code
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]));}} 

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.