DataAdapter object (data adapter)

Source: Internet
Author: User

The DataAdapter works closely with the dataset to create a memory representation of the data.

"The difference between DataReader and DataAdapter ":

The main reason is that they are connected to the database in different ways :

    The DataReader object closes the connection when the operation is no longer performed by opening a connection, then executing the SQL command, and then retrieving the data in the database;

    The DataAdapter object uses a data connection only when the DataSet object needs to be populated, and all server resources are freed when the operation completes.

The DataAdapter object is used to populate the dataset and update the database, and it can be thought of as a bridge connecting a dataset to a database to get or update data.

Common Properties: SelectCommand: The Command object that is used to select records in the data source.

"Common method": fill: Populates Data tables in a dataset;

Update: The data in the dataset is uploaded back into the database.

"Use of DataAdapter Objects":

1. Populating data sets

A, create database connection object;

b, create SQL command to query database;

C, create a DataSet object;

D, create DataAdapter to image;

"Statement": SqlDataAdapter SqlDataAdapter Object =new SqlDataAdapter (SQL command, database connection object);

E, call the Fill () method of the DataAdapter object to populate the DataSet object.

"Statement": SqlDataAdapter object. Fill (the name of the data table that the DataSet object needs to populate);

Note: If the data table that needs to be populated does not exist, the dataset will automatically create a data table and then populate the data, otherwise the data will be populated directly.

When you access data in a dataset, you need to parse the dataset.

1. Access the DataTable object in the DataSet object;

"Statement": DataSet object. table[table index or table name];

2. Access the DataRow object in the DataTable object;

Statement: TheDataSet object. table[table index or table name]. rows[row index];

3. Access a column of data in a DataRow object;

Statement: TheDataSet object. table[table index or table name]. rows[row index [column index or column name];

Note: The table index, the row index, and the column index all start at 0. An explicit type conversion is required when fetching data for a column in a DataRow object.

2. Update the data source:

A, create the CommandBuilder object, generate the relevant command to update the database;

"Statement": SqlCommandBuilder Sqlcommmandbuilder object =new SqlCommandBuilder (SqlDataAdapter object);

Note: TheCommmandbuilder object is used to retrieve the table structure and automatically generate the corresponding insert, update, and delete commands through the query commands of the DataAdapter object.

The DataAdapter Object query command must return at least one primary key or unique column, and if no primary key and unique column exists, a command failure will be generated and the data source cannot be updated.

B. Call the update () method of the DataAdapter object for updating the data source.

"Statement":SqlDataAdapter object. Update (the name of the data table that the DataSet object needs to submit);

DataAdapter object (data adapter)

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.