For more information, see ASP. NET DataReader object and asp. netdatareader.

Source: Internet
Author: User

For more information, see ASP. NET DataReader object and asp. netdatareader.

Recently, the DataReader object has been frequently used. It is actually used before for DataReader. To be honest, I personally think it is not easy to understand. In contrast, DataReader is useful when the DataSet object is much easier to use, but sometimes the retrieved data does not need much.

 

Here, I will explain DataReader. This is my personal understanding of DataReader. It is inevitable that there will be errors and you will learn from each other. I hope you can correct the mistakes and help me better understand them. Thank you.

 

Compared with DataSet, DataReader is an abstract class, so it cannot be used.

DataReader DR = new DataReader () to construct a function to create an object. To create a DataReader object, you must use the ExecuteReader method of the command object.

 

Second, the DataReader object can only read data in sequence and cannot write data (This DataSet occupies an absolute advantage ), the so-called sequential Data Reading is to read the rows in the data table from the beginning to the end. When a DataReader is created, the record pointer is at the front end of the table. You can use the Read method to Read a record from the table each time.

In general, DataSet stores two-dimensional arrays, while DataReader stores one-dimensional arrays. DataSet uses a non-connection transmission mode to access the data source. Once the data requested by the user is read into the DataSet, the connection to the database is closed, and the DataReader must be connected to the database at all times.

 

For me, important and common attributes and methods:

The FieldCount attribute is used to obtain the number of records contained in the DataReader object.

Close () Close the DataReader object

 

Actually there are various GetBoolean (col), GetChar (col), GetString (col), GetDateTime (col), GetInt32 () and so on to get the values of Columns with the serial number of col, these methods can be replaced by the GetValue (col) method.

GetValue (col) obtains the value of a column with the serial number col.

 

GetValues (values) obtains the values of all fields and stores the values in the values array.

I would not use the GetValues (values) method, especially where the values array came from, and why the type of the custom definition would come from Baidu. before using this method, object [] values = new Object [sqlrd. fieldCount]; this is the prerequisite for defining the values array.

 

Read () reads the next record, returns a Boolean value, and returns true, indicating that there is a record. This method is generally used together with the while () loop. DataReader object is the most important method.

 

Other irrelevant methods: (I don't know the specific data type when extracting data. Most of the data types used for viewing data are useful, or for searching for problems and debugging. I usually cannot use these methods)

GetDataTypeName (col) gets the source data type name of the column with the serial number col

GetFieldType (col) is used to obtain the data type of a column with the serial number col. Generally, the data type is System .**

GetName (col) gets the field name of the column with the serial number col

GetOrdinal (name) gets the sequence number of the column whose field name is name.

 

 

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 protected void Page_Load(object sender, EventArgs e)        {            string price = Request.QueryString["Price"];            string con = ConfigurationManager.ConnectionStrings["con"].ToString();            SqlConnection sqlcon = new SqlConnection(con);            string sql = "Select * from Cloths where price = '" + price + "'";            //string sql = "select * from Cloths";            sqlcon.Open();            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);            SqlDataReader sqlrd;            sqlrd = sqlcom.ExecuteReader();            Response.Write("Retrieve the number of record lines contained in the DataReader object. Note that the structure of the DataReader is different from that of the database. It converts a row of the database into a column :" + sqlrd.FieldCount);            Response.Write("<br>");            Response.Write("Obtain the source data type name of the column with the serial number 0 :" + sqlrd.GetDataTypeName(0));            Response.Write("<br>");            Response.Write("Obtain the data type of a column with the serial number 0 :" + sqlrd.GetFieldType(0));            Response.Write("<br>");            Response.Write("Obtain the field name of the column with the serial number 0 :"+sqlrd.GetName(0));            Response.Write("<br>");            Response.Write("Get the serial number of the column with the field name as the brand :"+sqlrd.GetOrdinal("Brand"));            Response.Write("<br>");            while (sqlrd.Read())            {                Response.Write("<br>");                Response.Write(sqlrd["id"] + "," + sqlrd["Brand"] + "," + sqlrd["Price"] + "," + sqlrd["Quantity"]);                Response.Write("<br>");                Response.Write("<br>");                Response.Write("The following shows why the serial number 0 is the brand rather than the id. Although the first column on the GridViewCommon page is the id, we must fully respect the database structure, we can see that the first column in the database is the brand! ");                Response.Write("<br>");                Response.Write("Here, we use the GetString function to retrieve data from columns with the serial number 0. here we need to select different Get functions based on different data types, which may be GetChar (), GetInt32 (), and so on :"+sqlrd.GetString(0));// Here we can see that the GetValue function can retrieve data without having to know the Data Type of each column in advance, so use the GetValue function whenever possible.                Response.Write("<br>");                Response.Write("Here, the GetValue function is used to retrieve the data of the column with the serial number 0, regardless of the data type. Note the following comparison :" + sqlrd.GetValue(0));                Response.Write("<br>");                Response.Write("<br>");                Response.Write("The GetValue () method is used to extract the displayed data :");                for (int i = 0; i < sqlrd.FieldCount;i++ )// Here, the data is retrieved through a loop. Actually, the GetValue () method is used cyclically.                {                    Response.Write(sqlrd.GetValue(i));// This is the step-by-step data extraction. The GetValue () method is used. Compared with GetValues, GetValues is in place in one step, just loop through the array.                    Response.Write("<br>");                }                Response.Write("<br>");                Response.Write("The GetVales () method is used to extract the displayed data :");                Response.Write("<br>");                Object[] values = new Object[sqlrd.FieldCount];// This step is very important. You must first define an array                sqlrd.GetValues(values);// Note that the GetValues (values) method is used here.                Response.Write(values[0]+","+values[1]+","+values[2]+","+values[3]+","+values[4]);                Response.Write("<br>");                for (int i = 0; i < sqlrd.FieldCount;i++ )// Here is the cyclic values array instead of the GetValues (values) method.                {                    Response.Write("<br>");                    Response.Write(values[i]);                    Response.Write("<br>");                }             }            sqlrd.Close();            Response.Write("<br>");            Response.Write(sqlrd.RecordsAffected);// Obtain the number of rows modified, inserted, or deleted by executing the SQL statement.            sqlcon.Close();        }


This is the design and content of the corresponding Cloths database:

 

This is:

Some may be interested in the string price = Request in the code. queryString ["price"] has doubts. In fact, this is called the hyperlink to transfer values or to transfer values between pages. This should not affect your reading and understanding, I will not upload the page corresponding to the uploaded value, because the connection is not big, it is just a value transfer.

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.