ADO. NET -- DataAdapter object

Source: Internet
Author: User

ADO. NET -- DataAdapter object

DataAdapter object

Source and function: In the ADO. NET object I introduced earlier, such as the Connection object, Command object, and DataReader object, all of these objects belong to the Data Provider and are connected-based. If we need to connect to the database every time we retrieve tables or rows in the database, the performance and efficiency are very low. In fact, ADO. NET provides a non-connected Core Component: DataSet. Where does the stored data set come from? ADO. NET provides the data adapter of DataApdater for DataSet.

Description:The DataAdapter object provides a bridge between the DataSet object and the data source., DataAdapter uses the Command object to execute SQL commands in the data source to load data to the DataSet, and coordinates the changes to the data in the DataSet back to the data source.

Working principle:Dataapter is essentially a data provisioner.. When we need to query data, it retrieves data from the database and fills in the DataSet or able to be local. When I need to update the database, it fills in the data in the local memory to the database, and execute the update command.

Function: DataAdapter is the most complex ADO. NET component because it is structured on all other DataProvider objects. The Connection object, DataReader object, Paramter object, and Command object serve it as much as possible. Generally, DataAdapter has three main functions:

1. Data retrieval:Fill in the data source to the local DataSet or able in the simplest way possible. Specifically, DataAdapter uses a DataReader instance to retrieve data. Therefore, you must provide a Select query statement and a connection string.

2. Data Update:Return the locally modified data to an external data source. When querying data from a database, we only need a basic Select statement. When updating a database, we need to differentiate the Insert, Update, and Delete statements.

3. Table or column name ing:Maintain the ing between the local DataSet table name and the external data source table name and the column name.

Attribute:

1. SelectCommand object: refers to the Command object retrieved from the data source.

2. UpdateCommand: refers to the Command object that writes modified rows from DataSet to the data source.

3. InsertCommand: reference the Command object that writes the inserted row from DataSet to the data source.

4. DeleteCommand: refers to the Command object for deleting rows from the data source.

Method:

1. Fill: Use the DataAdapter method to add or refresh rows from the data source and place these rows in the DataSet table. The Fill method calls the SELECT statement specified by the SelectCommand attribute.

2. Update: use this method of the DataAdapter object to transfer the changes to the DataSet table to the corresponding data source. This method calls the INSERT, UPDATE, or DELETE command for each specified row in the DataSet able.

Application Example

Public void SqlAdapterDemo (string connStr) {SqlConnection conn = new SqlConnection (connStr); // create a connection object SqlCommand cmd = conn. createCommand (); // create the SQL command object cmd. commandType = CommandType. text; cmd. commandText = "select * from products = @ ID"; // SQL statement cmd. parameters. add ("@ ID", SqlDbType. int); <span style = "font-family: KaiTi_GB2312;"> // Add parameters </span> cmd. parameters ["@ ID"]. value = 1; // assign a Value to the parameter of the SQL statement SqlDataAd Apter adapter = new SqlDataAdapter (); // construct the SqlDataAdapter adapter. selectCommand = cmd; <span style = "color: # FF0000;"> // bind to an SQL command object, which is indispensable </span> DataSet ds = new DataSet (); adapter. fill (ds); // Fill in data .}
It seems that the DataAdapter object is very large. Its use must be based on the Command object and Connection object mentioned earlier, and they are driving the "Data Source World" together ."

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.