Ado. NET Getting Started Tutorials (eight) in-depth understanding of DataAdapter (i)

Source: Internet
Author: User

Summary

Ado. NET has two core components: The data Provider component based on the connection and the non-connected dataset component. A connection-based data provider component is often used to retrieve data from a database in real time. A non-connected dataset, however, appears to have no direct connection to the database and is used only to store data tables or collections provided by data provider in local memory. It all seems so subtle, at this point, are you wondering: How are these two components linked together? Perhaps, by reading this article will more or less relieve some of your doubts!

Directory
    • Meet DataAdapter
    • How the DataAdapter Works
    • Talk about the important members of DataAdapter.
    • Summarize
1. Understanding DataAdapter

In the preceding object, such as the Connection object, the Command object, and the DataReader object, these objects are part of the data provider and are all based on connections . They have powerful features that allow you to easily connect to a specific data source, execute SQL statements, retrieve read-only data streams, and more. These connection-based objects correspond to a specific data source. In other words, for different data sources, we need to find the corresponding database provider (data Provider) to match them. of course, you don't have to be confused and nervous about it, I've already made it very detailed in front of you. Perhaps, with what we have learned at the moment, there is nothing wrong with manipulating data sources and retrieving data. However, this does not give the advantage of ADO. If we need to connect to the database every time we retrieve the tables or rows in the database, the performance and efficiency are very low. In fact, ADO. NET provides a core component based on non-connectivity: a DataSet. The dataset component lets us happily manipulate table-centric data collections in memory, just like tables in a database. It's so exciting and exciting!

I don't know if you've ever thought of such a problem (I was thinking about it anyway): Since the DataSet is based on a non-connected (no need to connect to the database), where does it store the collection of data? In fact, many times, its data comes from the database. Oh! This seems a bit contradictory! Make an "indecent" metaphor: like no chicken, where is the egg? you might as well take a different view of the problem! Although, you do not have chickens, but you can also buy eggs from the supermarket or other retail stores? Similarly, although the dataset is not directly connected to the database, ADO. NET has long prepared a very modest and friendly intermediary for the dataset:dataapdater. The Dataapater data adapter is one such object: It provides a solid bridge between the external data source and the local dataset collection, and the data retrieved from the external data source is properly provisioned into the local dataset collection .

2. How the DataAdapter Works

To say that DataAdapter is the most complex part of the world, in fact, not too! But we don't have to panic. Although the internal implementation mechanism of Dataadater is complex, it is simple to provide a developer interface. We know thatDataapapter is essentially a data-leveling device . When we need to query the data, it retrieves the data from the database and populates the dataset or DataTable to be local, and when I need to update the database, it routes the local memory data to the database and executes the update command. Let's take the Customer table as an example to understand how DataAdapter works. The working process of a DataAdapter is described in detail.

Figure 2.1 DataAdapter execution Process diagram

From what we can clearly know, when I query the customer information, DataAdapter first constructs a SelectCommand instance (essentially a command object), and then checks whether to open the connection, or open the connection if no connection is opened. The DataReader interface is then called to retrieve the data, and finally the retrieved database is populated into a local dataset or DataTable, based on the maintained mapping relationship. In the same vein, when we need to update the data source, dataadatper the locally modified data, maps the relationship, constructs the Insertcommand,updatecommnad,deletecommand object, and executes the corresponding command.

The reason for this is that DataAdapter is the most complex ADO component because it is architected on top of all other Dataprovider objects. Connection objects, DataReader objects, paramter objects, and command objects are served as much as possible. Overall, DataAdapter has three major functions:

    • data retrieval: populate the data source into a local dataset or DataTable whenever possible with the simplest method. To be careful, DataAdapter uses a DataReader instance to retrieve the data, so you must provide a SELECT query statement and a connection string.
    • Data Update: It is relatively slightly more complicated to return locally modified data to external data sources. Even when querying data from a database, we only need a basic SELECT statement, and updating the database requires distinguishing between the insert,update,delete statements.
    • table or column name Mapping: maintains a mapping of the local dataset table name and column name to the external data source table name and column name.

3. Talk about the important members of DataAdapter

As one of the. NET Dataprovider Object members, DataAdapter has similar characteristics to other data-providing objects: Both are connection-based and inherit from the base class, with different data sources corresponding to their own derived versions . In this sense, learning DataAdapter seems to be much simpler. The base class for DataAdapter is DbDataAdapter, which is structured as follows:


IDbDataAdapter, IDataAdapter, ICloneable

As we can see from the above, DbDataAdapter is an abstract base class that cannot be instantiated and inherits DataAdapter classes, Idbdataapdater,idataadapter, and ICloneable interfaces. DataAdapter members are more, must master the following several:

    • Selectcomand property: Gets or sets the command used to select records in the data source.
    • UpdateCommand Property: Gets or the command that is used only to update records in the data source.
    • DeleteCommand property: Gets or sets the command used to delete records from the data source.
    • InsertCommand property: Gets or sets the command used to insert a new record into the data source.
    • Fill method: populates the data set.
    • Update method: updates the data source.

4. Summary

In this paper, Imainly from "Why" and "what" point of view in detail the working principle of DataAdapter and the main role. Following the article, I will focus on"how" to analyze the practical application of DataAdapter in detail. If the article is helpful to the friends of the park, I hope to get your recommendation and attention . Your affirmation and support is my greatest motivation to continue writing!

Article posted: http://liuhaorain.cnblogs.com

Ado. NET Getting Started Tutorials (eight) in-depth understanding of DataAdapter (i)

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.