Memory fragments of ADO. NET (4)

Source: Internet
Author: User

 

Previous http://www.bkjia.com/kf/201112/112880.html

 

DataSet class

 

The DataSet object can be regarded as a Cache, which can keep the data queried from the database and even display the whole database. DataSet can not only store multiple tables, but also obtain data Table structures such as primary keys through the DataAdapter object and record the association between data tables. The DataSet object can be called ADO. NET heavyweight object, which is structured on the DataAdapter object and does not have the ability to communicate with the data source. That is to say, we use the DataAdapter object as the DataSet object and a bridge between data sources for data transmission.

You can create a DataSet object in either of the following ways:

1. DataSet ds = new DataSet ();

2. DataSet ds = new DataSet ("MyDataSet ");

The second method is to add a string parameter to initialize the name of the newly generated DataSet object. The first generated DataSet object has no name.

Use the Fill () method of DataAdapter to create the structure of the storage result for ds:

String strConn = "..."; // connection string

String strSql = "select * from MytableName1 ";

SqlDataAdapter da = new SqlDataAdapter (strSql, strConn );

DataSet ds = new DataSet ();

Da. Fill (ds, "MyTableName ");

This will save the query results in a DataTable object named MyTableName In the DataSet object.

Relationships between four classes

First, let's talk about the relationships between DataSet, able, DataColumn, and DataRow.

First, DataSet is like a database; DataTable is like a table in the database; DataColumn is like some information about the fields in the table, such as the field name and type; DataRow is like a record in the table, in addition, DataSet data can only be accessed through the row [Index], row [field name], or row [DataColumn object. The relationship between these two items is: DataSet contains multiple able; DataTable contains multiple datacolumns and multiple DataRow. The c # description is as follows:

 

DataSet ds = new DataSet ("MyDataSet ");

List <DataTable> tables = new List <DataTable> ();

List <DataColumn> columns = new List <DataColumn> ();

List <DataRow> rows = new List <DataRow> ();

Ds. Tables = tables;

Ds. Tables [0]. Columns = columns;

Ds. Tables [0]. Rows = rows;

// Ds. Tables [0] indicates the first table in the DataSet database.

 

Let's use the text to describe: www.2cto.com

A DataSet object has a Tables attribute that is a collection of able objects and stores all the DataTable objects.

A DataTable object has a Columns attribute that is a collection of DataColumn objects and stores all the DataColumn objects.

A ws able object has a Rows attribute that is a collection of DataRow objects and stores all the DataRow objects.

A DataRow object is like a record in a table. To access DataSet data, you can only use this DataRow object:

Row [Index], row [field name], or row [DataColumn object]

DataTable object

This object is similar to the DataReader object. You can view the query results. The results are published as a set of rows and columns. DataReader is designed for performance consideration. It can only view result sets in the stream mode, neither modify data nor return the previous row. It provides few functions. However, the DataTable object has more powerful functions to modify, sort, and filter data. Two main attributes are Columns and Rows.

DataColumn object

The DataColumn object defines the DataTable architecture. After calling the Fill () method of DataAdapter, DataAdapter also creates a DataColumn object for each column in the query result, which has the basic attributes of Name, Ordinal, and DataType. View these attributes:

String strConn = "..."; // connection string

String strSql = "select * from MytableName1 ";

SqlDataAdapter da = new SqlDataAdapter (strSql, strConn );

DataSet ds = new DataSet ();

Da. Fill (ds, "MyTableName ");

Foreach (DataColumn col in ds. Tables ["MyTableName"]. Columns)

{

Console. WriteLine ("{0} -- {1}", col. ColumnName, col. DataType );

}

DataRow object

When we want to view or modify the data in the DataTable, we must use the DataRow object. Traverse the first column of data in the DataTable:

Foreach (DataRow row in ds. Tables ["MyTableName"]. Rows)

{

Console. WriteLine ("{0}", row [0]);

}

Compile a parameter that accepts a row, and display the column name and value of this row:

Static void DisplayRow (DataRow row)

{

Foreach (DataColumn col in row. Table. Columns)

{

Console. Write ("{0 }:{ 1}", col. ColumnName, row [col]);

}

}

You can use DisplayRow () to traverse all data in a Table ():

Foreach (DataRow row in ds. Tables ["MyTableName"]. Rows)

{

// Display the row number

Console. Write ("{0}", ds. Tables ["MyTableName"]. Rows. IndexOf (row ));

// Display row data

DisplayRow (row );

Console. WriteLine (); // line feed

}

So far, we have found that the DataTable object, DataColumn object, and DataRow object we are using are automatically generated after the DataAdapter Fill () method is called. The next example is: how to manually operate these objects.

 

Author: kiss you

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.