Detailed description of DataReader In ADO. NET, ado. netdatareader

Source: Internet
Author: User

Detailed description of DataReader In ADO. NET, ado. netdatareader
Features

Schematic diagram

PS: Read () moves the pointer down and destroys the previous one. Therefore, SqlDataReader is only input.

GetValue () is used to find columns in the current row.

SqlDataReader () feature.

1) The above schematic is described.

2) read-only SqlDataReader can only be obtained and cannot be modified.

3) when using SqlDataReader, you must ensure that the Connection status is enabled.

Because the result set is stored on the database server, it cannot be found if the connection is not enabled.

Data Reading Method

1. reader.GetValue (0) 

There is no column name overload.

2.Reader [1] 

The indexer is implemented using GetValue (0.

3. reader ["column name"] 

Internally implemented through GetOrdinal ()
// First obtain the index int index = reader. GetOrdinal (columnName); // then find the corresponding column through the index. Reader. GetValue (index);/*** note **: The execution efficiency of reader ["column name"] is very low and is not recommended. Because if such a statement is in a loop, how many times will it be executed? int index = reader. GetOrdinal (columnName );*/
If the column name must be used in the project, we recommend that you do not write it in a loop.
// Write the retrieved column name in the out-of-cycle header. Int c1 = reader. GetOrdinal ("column name"); while (reader. Read () {object obj = reader [c1];}
Obtain strong data types

 

Reader. GetString (0); reader. GetINT32 (1); reader. GetDouble (2); // float in the database. Double is used here.

 

Why do we need to obtain a strong type?

This is because when an Object is used, it needs to be split once, with performance loss.

DataReader reads two result sets
Do {if (reader. HasRows) {while (reader. read () {/* read data */} // if there are other result sets, continue the loop! While (reader. NextResult ());
Dynamic acquisition of strong types
If (reader. hasRows) {while (reader. read () {// traverse the column for (int I = 0; I <reader. fieldCount; I ++) {// obtain the data type string dbType = reader. getDataTypeName (I); switch (dbType) {case "varchar": case "nvarchar" case "char": case "nchar": reader. getString (I); // abbreviated as break; case "int ":......}}}}
IsDBNull

When the column in The result set is null and needs to be obtained with a strong type, you need to use IsDBNull to determine.

  string obj = reader.IsDBNull(0)? "" : reader.GetString(0);

 

 

 

 

Related Article

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.