Ado.net Designed for ADO programmers (1)

Source: Internet
Author: User
Tags join ole domain
ado| Program | programmer | Designed for ADO programmer Ado.net

Summary:This article discusses how to implement basic database operations in a ado.net manner and when to use ado.net instead of ADO.

Directory


Data access in. NET
Reading data
Datasets, DataTable, and Recordset
Convert existing code
Update data
XML Extension Support
Summarize
Since the introduction of the Open Database Connectivity (ODBC) application Programming Interface (API) Several years ago, a variety of database access technologies have emerged, and Ado.net is the newest of them. In the process, a lot of interesting things happened. For example, COM intruded into the database domain and began to cultivate the colonization process of OLE DB. Then, Activex®data Objects (ADO), roughly equivalent to the OLE DB Automation version, was chosen to govern the Visual basic® and ASP communities of Windows® database developers.
Through. Net,microsoft is providing a common framework (the framework Class Library), which will include all existing Windows APIs and even more content. It is particularly noteworthy that it includes a large number of commonly used libraries, which now need to be obtained separately from each COM object. In these libraries, you'll find XML and ADO object models that are integrated into a class subtree called ado.net.
Ado.net is actually the basis for building data-aware. NET applications. Unlike ADO, Ado.net follows more general principles and is less specifically oriented to the database. Ado.net sets up all classes that allow data processing. These classes represent data container objects that have typical database functions, such as indexes, sorting, and views. Although Ado.net is the authoritative solution for. NET database applications, it is not as central to the database as the ADO model in the overall design, which is a major feature of the ado.net.
Ado.net differs greatly from ADO. Ado.net is a new data access programming model, which needs the comprehensive understanding, input and new thinking of the developer. However, once you start mastering ado.net, you will realize that the original ADO techniques are very helpful in creating effective applications and resolving old problems in different, yet more ingenious, and reliable ways.
In the remainder of this article, I will focus on how to implement basic database operations in a ado.net manner. I want to explain when Ado.net is a better choice than ADO, and when you should give up ADO. Ado.net is not an improvement in ADO to conform to the. NET infrastructure. As long as you look at Ado.net's syntax, code design, and porting, you'll understand this. data access in. NET
The way in which data sources are accessed in ado.net is determined by the hosting provider. Functionally, managed providers are very similar to OLE DB providers, but there are two important differences. First, the management provider works in the. NET environment, retrieving and exposing data through. NET classes such as DataReader and DataTable. Second, because their architectures are optimized for. NET, they are relatively simple.
Currently Ado.net provides two types of managed providers: one for SQL server™7.0 or later, and one for all other OLE DB providers that you might have installed. In both cases you use separate classes, but follow similar naming conventions. Names are the same except for prefixes. The previous case is prefixed with SQL, and the latter is ADO.
You should use SQL classes to access SQL Server tables because they go directly to the internal API of the database server and skip the middle tier represented by the OLE DB provider. The ADO class is a. NET interface on an OLE DB provider that works using COM Interop bridges.
Beginners of Ado.net objects can refer to Omri Gazitt's article ado+: Data access Services for the Microsoft. NET Framework (English) and my ado+ promote the evolution of data types (English). The former is highly technical and provides a high-level commentary overview for the Ado.net program model. The latter focuses on the goals of Ado.net and its links to XML, scripting, and other technologies. reading Data
The ado.net application that needs to read data from the data source first creates a connection object. Depending on the destination provider, the connection object can be a SqlConnectionOr adoconnection。 Keep in mind that you can use the Ado.net class to connect to the SQL Server database, but we do not recommend doing so. The only drawback is that your code is going through unnecessary extra layers of code. It first calls the managed provider for ADO, and then the managed provider invokes the SQL Server OLE DB provider. SQL Server managed providers, like OLE DB providers, manipulate data directly.
The significant difference between ADO and Ado.net connection objects is that the Ado.net connection does not support CursorLocationProperty. Note that this is not a document error, but a controversial design issue. To highlight the data-centric principle, ado.net does not have an explicit implementation of the cursor.
In ADO, you are accustomed to using cursors to extract records from a database or any other OLE DB-compliant data source. You can select client or server cursors, each of which has several preset cursor types. Ado.net is designed to extract data from a data source and to provide a new programming interface for reading and analyzing data.
In ADO, you create the connection and command text by specifying the RecordsetObject. The Recordset has a strategy for the location and type of the cursor. You can read data in one of the following ways:
    • Creates a static copy of the selected records in memory, and then processes the records as necessary when disconnected from the data source. ADO is called a static cursor.

    • Scrolls data through a fast, forward-only, read-only cursor that works in a static snapshot of a record. ADO is called a read-only cursor.

    • Data is accessed through two server-side Grand, which require a good connection, but you can detect changes to other connected users at all levels at any time. ADO calls them keyset and dynamic cursors.





The first two methods work in disconnected recordsets and read information from the client cache, which is a similarity. In addition, in a Web-oriented environment and for new NLayer systems, both of which are proven to be the most frequently used.
In ADO, all of these methods correspond to different types of cursors. You will find later in this article that although ado.net is very different, it can implement any functionality that you can implement with ADO. But your code extracts data from the actual data source and its physical storage medium and format.
Ado.net provides two objects to handle data extracted from the data source. They are DataSetAnd DataReaderObject. The former is a memory-logged cache that you can access and modify in any direction. The latter is a highly optimized object designed to scroll only read-only records in forward mode. Please note that DataSetLooks like a static cursor, but in fact, in. NET, it corresponds to an ADO read-only cursor DataReaderObject.
Server-side cursors are not supported in Ado.net. However, this does not mean that you cannot use cursors. All you need to do is import the ADO type library in. NET. In the Project window ReferencesRight-click on the node is OK. After you import, you can begin to use the local ADO object in your application.
Although I admit that it is difficult to make a decision to turn to. NET, I personally recommend that you consider overriding an existing application with. Net. You can take the full import of ADO as the first step toward. NET without spending too much time and resources. However, keep in mind that this is only the first step on a long road. This is by no means the only step toward. Net. The real reason for the value of. NET is a unified and consistent programming interface and extensive use of local classes. You can import a COM type library, but importing a COM type library can only be a temporary solution or intermediate step, and we do not encourage it.
When using ado.net, you should fully consider the fact that it unifies the data container class programming interface. Whether you're writing an application, a Windows form, a Web form, or a Web service, you can work with the data through the same set of classes. Regardless of whether the data source at the back end is a SQL Server database, OLE DB, XML file, or an array, you can scroll through the same methods and properties and work with their contents.

Adonetdev01.gif


Figure 1:solution Explorer Menu
If you insist on using ADO in. NET, be prepared to face some side effects. For example, you need additional code to be able to use a recordset from a data-bound control. datasets, DataTable, and Recordset
In the ado.net, there is no Recordsetobjects that correspond directly to the object. The closest thing is DataTableObject. Although the functions of these two objects are almost the same, they play a different role in their respective frameworks.
Recordsetis a large object, with many ADO functions, but still somewhat deficient. RecordsetPerformance is good in many ways, such as being able to create, still work when disconnected, feature-rich, and so on. However, in some respects it still needs to be improved. For example, because RecordsetInherent COM features, the task of serializing over the network will be onerous. And as a binary object, it's hard to share a module running on a different platform, and it can't go through a firewall. Other than that RecordsetRepresents a single table for multiple records. If the table is generated by one or more JOIN, it can be difficult to update the original data source. If you want to keep the disconnected recordset and the original data source in harmony, the data source must be able to recognize SQL. However, your recordset is most likely created by a non-SQL provider.
In Ado.net, all the features of the ADO Recordset are split into simpler objects, DataReaderis one of them. DataReaderSimulates the operation of a fast, forward-only, read-only cursor.
DataTableis a simple object that represents a data source. You can manually construct DataTable, you can also pass DataSetcommand to populate it automatically. DataTableDoes not distinguish the source of the data it contains. This object allows you to work with data in memory and to perform actions such as browsing, sorting, editing, applying filters, creating views, and so on.
In ADO, there is no DataSetThe corresponding object. DataSetobject is a container class, and it is the key object to implement Ado.net data extraction. DataSetAdd one or more DataTableobjects are grouped. DataTableExposes its contents through a universal collection like rows and columns. When you try to read data from a datasheet, you may pass through two different object tiers: DataTableMappingAnd DataView
DataTableMappingObject describes the data in a data source columns DataTableThe mapping relationship between objects. When the fill DataSetWhen DataSetCommandobject to use this class. It maintains links between the abstract columns in the dataset and the physical columns in the data source.
The view of the table is passed DataViewObject implementation. It says DataTableCustom view that can be bound to specific controls, such as the data grid in Windows forms and Web forms. This object corresponds to the implementation of the SQL CREATE VIEW statement in memory.
DataSetAll tables in can be placed in a relationship through a common domain. This relationship by DataRelationObject Management. This looks like ADO data formation, but there is one important difference. You don't need to use data to form a language, and you end up with a very flexible architecture. The Ado.net navigation model makes it easy for you to move from the main row in a table to all of its child rows.
DataRelationObjects are equivalent to the implementation of a JOIN statement in memory, and can be used to establish a parent/child relationship for a column of the same data type. Once a relationship is established, any changes that can break the relationship are not allowed, and if they occur, they can result in a Run-time exception. Views and relationships are two ways to implement a master table/BOM schema. Remember that a view is just a mask placed on a record, and a relationship is a dynamic link that is set between one or more columns of two tables. If you use relationships, you cannot change the order or set conditions.
If your code requires a one-to-one foreign key relationship and does not change the data, then it is best not to use the unformatted JOIN command. If you need additional filtering capabilities, you should use the Ado.net custom view.

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.