Details of the DataReader object of ASP.

Source: Internet
Author: User

Recently frequently used DataReader This object, in fact, for DataReader, before also used, to tell the truth I personally feel very difficult to understand. In contrast, the DataSet object is much more useful, but sometimes the data taken out does not need a lot of time, DataReader is still helpful.

In this DataReader to explain, this is also a personal understanding of DataReader, inevitably there will be mistakes, we learn from each other. There are mistakes in understanding and I hope you can correct them, help me understand better, thank you.

First DataReader is an abstract class compared to dataset,datareader, so it cannot be used

DataReader DR = new DataReader (), to construct a function to create an object, if you want to create a DataReader object, you must pass the Command object's ExecuteReader method.

Second, the DataReader object accesses the data in a sequential way, only reading the data sequentially, and cannot write to the data (this dataset occupies an absolute advantage), so-called sequential read data is the data table of the row from beginning to last read. When DataReader is created, the record pointer is at the front of the table, and you can use the Read method to read a record from the table each time.

In general, the DataSet is a two-dimensional array, and DataReader is a one-dimensional array. And the dataset uses a non-connected transport mode to access the data source, once the user requests the data read into the dataset, the connection to the database is closed, and the DataReader is always maintained with the database connection state.

As far as I'm concerned, important and commonly used properties and methods:

FieldCount property gets the number of record rows that the DataReader object contains

Close () closes the DataReader object

In fact there are various Getboolean (COL), GetChar (col), GetString (col), GetDateTime (col), GetInt32 () and so on to get the value of the column ordinal to Col, and these can basically be getvalue ( Col) method is replaced.

GetValue (COL) Gets the value of a column with the ordinal col

GetValues (values) Gets the values of all fields and stores the field values in the values array

The GetValues (values) method is not used, especially for the values where the array is coming in, and if you customize the definition of why the type, then Baidu will know, before using this method, object[] values = new object[ Sqlrd. FieldCount]; This is the precondition for defining the values array.

Read () reads the next record, returns a Boolean, and returns True, indicating the next record. This method is generally used in conjunction with the while () loop. is one of the most important methods of DataReader objects.

Other innocuous methods: (I think this when extracting data, do not know the specific data type, used to see the data type of the role of the majority, or to find the problem debugging it.) Anyway, I don't usually use these methods.

Getdatatypename (COL) Gets the source data type name of the column with the ordinal col

GetFieldType (COL) Gets the data type of the column with the ordinal col, which is typically displayed as system.**

GetName (COL) Gets the field name of the column with the ordinal col

GetOrdinal (name) Gets the ordinal of a column with a field name of name

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 protected void Page_Load(object sender, EventArgs e)        {            string price = Request.QueryString["价格"];            string con = ConfigurationManager.ConnectionStrings["con"].ToString();            SqlConnection sqlcon = new SqlConnection(con);            string sql = "select * from Cloths where 价格 = ‘" + price + "‘";            //string sql = "select * from Cloths";            sqlcon.Open();            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);            SqlDataReader sqlrd;            sqlrd = sqlcom.ExecuteReader();            Response.Write("获取DataReader对象包含的记录行数,注意这里DataReader的结构跟数据库的结构是不一样的,它把数据库的一行转换成了一列:" + sqlrd.FieldCount);            Response.Write("<br>");            Response.Write("获取序号为0的列的来源数据类型名:" + sqlrd.GetDataTypeName(0));            Response.Write("<br>");            Response.Write("获取序号为0的列的数据类型:" + sqlrd.GetFieldType(0));            Response.Write("<br>");            Response.Write("获取序列号为0的列的字段名:"+sqlrd.GetName(0));            Response.Write("<br>");            Response.Write("获取字段名为品牌的列的序号:"+sqlrd.GetOrdinal("品牌"));            Response.Write("<br>");            while (sqlrd.Read())            {                Response.Write("<br>");                Response.Write(sqlrd["id"] + "," + sqlrd["品牌"] + "," + sqlrd["价格"] + "," + sqlrd["数量"]);                Response.Write("<br>");                Response.Write("<br>");                Response.Write("下面的显示要注意,为什么序列号为0的是品牌而不是id,虽然GridViewCommon页面第一列是id,不过这里是要充分尊重数据库的结构,可以看出数据库中第一列是品牌!");                Response.Write("<br>");                Response.Write("这里通过GetString函数取出序号为0的列的数据,这里要根据数据的不同类型选用不同的Get函数,可能是GetChar(),GetInt32()等等:"+sqlrd.GetString(0));//这里可以看出GetValue函数可以不需要事先知道每一列的数据类型就可以取出数据,所以尽量用GetValue函数                Response.Write("<br>");                Response.Write("这里通过GetValue函数取出序号为0的列的数据,不需要管数据时什么类型,这里注意跟上面的比较:" + sqlrd.GetValue(0));                Response.Write("<br>");                Response.Write("<br>");                Response.Write("这里是运用GetValue()的方法提取显示数据的:");                for (int i = 0; i < sqlrd.FieldCount;i++ )//这里通过循环取出数据,其实就是循环使用GetValue()方法                {                    Response.Write(sqlrd.GetValue(i));//这个是一步一步取数据,用的是GetValue()方法,相比于GetValues,GetValues是一步到位,只要循环数组就好                    Response.Write("<br>");                }                Response.Write("<br>");                Response.Write("这里是运用GetVales()方法提取显示数据的:");                Response.Write("<br>");                Object[] values = new Object[sqlrd.FieldCount];//这一步是很重要的,你要先给定义一个数组                sqlrd.GetValues(values);//注意这边是用的是GetValues(values)方法                Response.Write(values[0]+","+values[1]+","+values[2]+","+values[3]+","+values[4]);                Response.Write("<br>");                for (int i = 0; i < sqlrd.FieldCount;i++ )//这里是循环的values数组,而不是GetValues(values)方法                {                    Response.Write("<br>");                    Response.Write(values[i]);                    Response.Write("<br>");                }            }            sqlrd.Close();            Response.Write("<br>");            Response.Write(sqlrd.RecordsAffected);//获取执行SQL语句所更改,插入或删除的行数            sqlcon.Close();        }


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

This is:

There may be classmates for the string price = request.querystring["Prices" in the code, there is doubt, in fact, I call the hyperlink value or the page between the value bar, this should not hinder everyone's reading and understanding, it corresponds to the value of the page I will not come up, Because the connection is not big, just a pass value.

Details of the DataReader object of ASP.

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.