C #. NET uses SqlDataReader note a few----turn

Source: Internet
Author: User

1, the database connection will remain open until SqlDataReader is closed, so when using SqlDataReader, you should call Sqldatareader.close () to close it immediately. 2, a connection can only be used by a SqlDataReader, which is why to close the SqlDataReader as early as possible reasons.
3, after using SqlDataReader, you can display the call in the programDatabase ConnectionThe Close method of the ExecuteReader object closes the connection, or you can pass the CommandBehavior.CloseConnection enumeration variable when calling the Command object's method. This automatically closes the database connection when the Close method of the SqlDataReader is called.
4, use SqlDataReader when possible to use and database field type matching method to obtain the corresponding value, such as for the shape of the field using GetInt32, the character type of the field use GetString. This reduces the additional type conversion operations that are added because of inconsistent types.
5, when using SqlDataReader to get multiple records, if you do not have access to the end of the fetch record to close SqlDataReader, you should first call the Command object's Cancel method, and then call the SqlDataReader Close method. The Cancel method of the Command object makesThe database no longer sends the SqlDataReader data to the caller, and if you do not call this method to shut down SqlDataReader directly, the database sends and SqlDataReader data that is not accessed, such as a long empty data stream to the caller.
6. If you want to get the return value or output parameter of a stored procedure by SqlCommand's ExecuteReader method, you must first call the Close method of SqlDataReader to get the value of the output parameter or the return value.
7. If you use SqlDataReader to return only one record, when you call the command's ExecuteReader method, specify
Commandbehavior.singlerow parameter, the use of this parameter has no effect on SQL Server. NET data provider, but when you use OLE DB. NET data provider, when you specify this parameter , the Dataprivider internally will use the IRow interface instead of using a relatively resource-intensive IRowset interface.

C #. NET uses SqlDataReader note a few----turn

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.