ADO.NET will become the basis for building a data perception. Net application. Different from ADO, ADO.NET is more versatile, not that design for databases. ADO.NET aggregate all data processing Class. These classes present Data Container Objects with typical database functions, such as index, sort, browsing. Although ADO.NET is an important .NET database application solution, it is more showing comprehensive design Not only is the same as the ADO model as a database center. .
ADO .NET is very different from ADO. It is a new access programming model. When you start using ADO.NET, you will find anything you master on the ADO skills in building effective procedures and solve problems. There is a great help to you, you can help you develop more flexible and more securely in the problem.
ADO.NET is not ADO to improve the .NET basic constructs. When you slowly understand the syntax of ADO.NET, you will be clear after the code design, and transplantation.
Data Access in NET
Accessing the data source in ADO.NET is controlled by the hosting provider. Although the host DB has two major differences from OLE DB, both are extremely similar. First, the hosted provider runs in the .NET environment, Search and display data through the DataReader and the DataTable .Net class. Second, their architecture is relatively simple because it is optimized for adaptation .NET.
At this point, ADO.NET is divided into two different types of managed providers: a kind of SQL Server® 7.0 or higher, another suitable for all OLE DB providers you may have already installed. Although use in two The category in the hosted provider is different, but they follow similar naming methods. Other names are the same outside the prefix. The previous case is SQL, the latter is ADO.
You need to use the SQL class to access the SQL Server table, because the SQL class skips the intermediate layer presented by the OLE DB provider, and directly enter the internal API of the database server. ADO class is a .NET interface located at the top of the OLE DB provider. The COM Intero Bridge is working.
About Ado.Net Getting Started Know, you can read the article of Omri Gazitt's, and the article mainly introduces ADO :
About Microsoft's Database Access Services (Data Access Services), and the discussion of ADO in my article mainly points out the evolution of data species. The former is more purely technically, and provides a higher level about ADO.NET Overview of the programming model. The latter is mainly to explain ADO.NETR's goals and links with XML, scripts, and other technologies.
2. Read the data
ADO.NET Apps To read data from the data source, you must create a connection object. This connection object can be SQLConnection or Adoconnection, depending on the target provider used. Need to keep in mind, although here Not recommending, but you can also use the ADO .NET class to connect to the SQL Server database. This method is unique, the code needs to pass an unnecessary extra code layer. First it will transfer the ADO's hosted provider, then ADO's hosted provider calls the SQL Server OLE DB provider. Like the OLE DB provider, the SQL Server managed provider directs data directly.
About ADO and ADO.NET on the connection object is the biggest difference in the ADO.NET connection does not support the CursorLocation property. It is not as good as it is a document BUG, it is better to think that this is a design problem of a ready-to-have controversial. To enforce it The data is a centered benchmark, ADO.NET has no display implementation of the cursor.
In ADO, you are accustomed to extracting records using cursors from databases or other OLE DB-compatible data sources, you can choose the client or server-side cursor, each of which has several pre-set cursor types. More in ADO.NET is to extract data from the data source and provide new programming interfaces for reading and analyzing data.
In ADO, you can create a RecordSet object by specifying connection and command text. There are some rules for the location and type Recordset of the cursor. You can read the data as follows.
Create a static copy of the selected record in memory, and then processes the copy when disconnected from the data source, and the ADO is called a static cursor.
By fast, forward, read-only cursor is scrolling data, ADO calls only forward cursors. Access data through server-side cursors, you need a good connection, but you can detect from other connections on different levels. The information entered by the line user, ADO is called: dynamic cursor.
Both of the head are operated on the disconnected Recordsets and read information from the client cache, and the first two ways are often used in the Web-oriented environment and the new N-Tier system.
In ADO, all of these ways correspond to different types of cursors. In the text, you will find that ADO.NET is different from ADO, but it has all the features of ADO. Relative photo, your code will Data is extracted from the actual data source and its physical storage medium and format.
ADO.NET enables DataSet and DataReader to extract data from the data source. The former is a cache recorded in memory, you can access and modify from any direction. The latter is a highly optimized object, in a read-only record concentration Move forward only. Note: DataSet looks like a static cursor, but in fact, in .NET, the DataReader object corresponds to the ADO read-only cursor.
In ADO.NET, although no support is provided for the server-side cursors, this doesn't mean that you can't use a cursor. In fact, the step you need to do is to enter an ADO library in .NET. You only need to be in References Click Right click on the Node to run a local ADO object in your own program.
But I personally think that when you want to turn to .NET, please consider carefully. First, please enter the ADO completely, this will not spend too much time and energy, this is the first step toward .NET,. But, This is just the first step of the long march and also one step to .NET must have. The true value of .NET is based on a uniform, sustained and stable interface, and the wide application of local Classes. About Com Libraries is It can be supported, reasonable, but not encouraged because it is just a short-term solution, or a transitional step.
When you want to start using ADO.NET, consider this fact: ADO.NET unified data container class programming interface, so, no matter what type of program: Windows Form, Web Form, or Web Service, You have to concentrate on relevant data in the same group. Regardless of the backend data source is SQL Server Database, or the OLE DB provider, the XML file, or array, you can use the same method and properties. Treatment.
Figure 1. Solution Explorer Menu
If you insist on using ADO in the .NET world, please prepare to face some other impact, for example, you need additional code to use RecordSet from the data binding control.
3.Dataset, DataTable, and Recordset
About RecordSet Object., ADO.NET does not correspond to it directly. The closer is the DataTable object. Although they almost have the same function, they play different roles in their respective frameworks.
Recordset is a considerable object that has most features of ADO, but there is still a lack in some respects. Recordset is excellent in some aspects, such as: it can be creative, it can operate offline, but in some aspects It still needs to be improved, such as: Based on its inherent COM characteristics, RecordSet is difficult to serve on the network; Recordset is a binary object, so the modules between different platforms are difficult to share; and there is a snake that cannot pass through the firewall. In addition, it performs a single table of records. If the table is the result of one or a few join, it is difficult to update the original code source. When you try to unite the line of RECORDSET with the original code source, the data source You must be able to identify SQL. No matter what, your Recordset can be created by a non-SQL provider.
In ADO.NET, all features of Ado Recordset are separated into several simpler objects: one of them is DataReader. DataReader simulates the operation of fast, read-only, forward read-only cursors.
DataTable, a data source is a simple object. You can manually construct a DataTable, or you can use the DataSet command to generate. DataSet is not much better for the data it contains. By it, you can process data in memory, Or others such as sorting, editing, filtering, create browsing, etc.
The DataSet object is a data container class that is a key object that implements ADO.NET data. DataSet sets one or several DataTable objects. DataTable, through such a general collection, public content. When you try from data When you read the data, you may pass two different levels: DataTableMapping and DataView.DataTableMapping objects contain data columns in the data source, as well as the mapping relationship between DataTable Object.
When populating DataSet, the DataSetCommand object uses this class. It maintains links between the abstract columns in the data set and the physical columns in the data source.
The view of the table is implemented by a DataView object. It represents a custom view of a DataTable that can be bound to a specific control (such as a Windows Form and a data grid in a web form). This object is equivalent to the implementation of the SQL CREATE VIEW statement in memory.
All tables in the DataSet can be associated with each other through a common domain. The links between them are managed by the DataRelelation object. This is quite like ADO's data, but there is a maximum difference.
In DataRelation, you don't need to use data to form language, and you can also get a very flexible organizational architecture. Through the ADO .NET navigation model, you can easily move from the primary bank in a table to all of its sub-lines.
Dataralation Object is the implementation of the JOIN statement in memory, which can be used to establish the same Parent / Child relationship of the same data type. Once the relationship is established, any modification of this relationship is disabled. Views and rates are completed Master / Detail architecture Two methods. Please remember that the view is only loaded on the record above the record, but the RELATION
It is a dynamic link between several columns. Under the relation, you cannot change the order or set the condition.
If your code requires 1 pair of foreign key relationships, and change data, it is best not to use the Join command. If you need additional filtering, you can seek support for ADO .NET custom views.
4. Convert existing code
A large number of ASP pages use ADO objects to extract data. Let us review the typical cases of the next few cases, which may help when you process your transplant or override the code.
If you have an ASP page generated from a single Recordset, then DataReader will be your good helper.
{String strConn, strCmd; strConn = "DATABASE = MyAgenda; SERVER = localhost; UID = sa; PWD =;"; strCmd = "Select * From Names where ID =" contactID.Text; SQLConnection oCN = new SQLConnection (strConn) SQLCOMMAND OCMD = New SQLCOMMAND (STRCMD, OCN); OCN.Open (); SqlDataReader DR; OCMD.Execute (OUT DR); While (Dr.Read ()) {// Use Dr.getstring (Index) OR // DR ["Field Name"] to response.write data} You can use the HasMorerows property to quickly check if DataReader is empty. If you simply handle a series of records, there is nothing than DataReader. Faster, better object, It also applies to query a single record.
DataReader. The content is not editable, but you can move the content into a more manageable object, such as:
DataTable or one or more DATAROW objects.
DataReader is not a suitable tool when you need to handle the complex relationship between the table and the record. The more data model links, the more complicated the SQL command. Navigation modules have continuity, and finally put into cache data is often more than what you need,. DataSet and Dataralation Objects are the basis of this type of relational model. To manage the Parent / Child relationship, ADO is also the same for Data-Shaping Engine. Package. That is, Data Shaping and ADO .NET relationships are one thing. In terms of design, both have little common point. Shaped RecordsetSCT embedded list object includes all data table information. ADO.NET relationship is dynamic link, you can build at any time in two datasheets. Ado relies on the Shaping Ole DB Service Provider and uses a dedicated SQL class language feature to generate a layering during the execution of a single ADO command. Recordset.
In ADO.NET, each object involved in the relationship is always seen as a separate individual. The relationship itself is disclosed as an object and has a certain behavioral rule. For example, the DataRelelation object can be changed from the father to the subline layer. You can do this by adding the ForeignKeyConstraint object to the CONSTRAINTS collection of the DataTable. The ForeignKeyConstraint object indicates a set of columns associated with the foreign key relationship when deleting or updating the values and rows. As mentioned earlier, once the relationship is set, you cannot perform changes that may undermine the relationship before it is terminated by the program preset.
As mentioned earlier, once it sets RELATIONSHIP, unless it is a procedural termination, you cannot modify it, which will suddenly interrupt it.
In addition, Relations have no deferredity. You can set two different relationships between Customers and ORDERS, ORDERS, and Products. However, when you navigate for a Customer, you cannot jump from one Order to Related Products rows. Solution is to open the Orders / Products relationship, lock the Order you need, and get the relevant row.
The programmer needs to store records in the ASP Session? With ADO .NET and DataSet objects, you can work very well without causing the "Storing An Ado Recordset In Git Might Cause An Access Viocation" Trouble.
5. Update data
The web program typically updates the data using a non-manifest statement or by parameter processing. However, when you meet the downtown data, you may want to use the built-in service to update all the required revisions. To complete this work. ADO Batch update mechanism.
The UpdateBatch method is used to send the RECORDSET change stored in the copy buffer to the server to update the data source. It uses an open lock that allows all pending local changes. It also transmits all changes to the data source in a single operation. Open lock occurs only when the data source locks to change after the submission. Open lock allows two users to access the same record at the same time, but the changes entered by a user will soon be overwritten by another user. Of course, this approach requires data sources to detect and prevent data conflicts. The entire data source is also required to be more stable and there is no frequent changes. Otherwise, it is not difficult to imagine that coordination cost will soon exceed the savings caused by the replacement strict lock. In fact, use the UpdateBatch method to return an error when any changes fails. You can then access the error through the ErrorS collection and an Error object.
It is important to understand why the ADO.NET model is a more powerful tool for updating data, understanding the working principle of open-locked in Ado is critical. In the ADO code, you can't control everything that happened after calling UpdateBatch. That is, the update is performed on the server by scrolling the changed row, and then compares the current value in the corresponding record in the original value, and the data source. When all values are consistent, the appropriate SQL statement (INSERT, UPDATE or DELETE) is performed on the table. The above statement shows that you cannot control the SQL statement. The update code located in the server is neither better than yourself, nor does it work in the case of the non-SQL provider you use. At the beginning of this chapter, I have already told the web application is a typical process for updating the data by parameterizing the storage process. Anyway, if you use a batch update, the situation will be different.
In ADO.NET, the model has been expanded. Now it uses a more generic architecture, through which you can specify your own command statement, such as insert, delete, update, and select. More obvious, You can observe the attempt to extract data from the data source, and regardless of the nature of the data source, you can provide the same support. ADO.NET's batch update, ask you to create a DataSetCommand object: SqlDataSetCommand or AdodatasetCommand
Note: In Beta 2, DataSetCommand objects are called DataAdapter objects.
Once you have a DataSetCommand object, you can use its Update method. DataSetCommand provides a range of properties: such as InsertCommand, DeleteCommand, UpdateCommand, And SELECTCOMMAND. They are all Command objects, but you can't set them unless default The settings are not completed in your request. This is the same as the ADO. During the Update process, if the xxxCommand property is set, the primary keyword already exists, the Command object is automatically generated.
The following code shows how to set the primary key for EmployeesList Table,
Datacolumn [] keys = new datacolumn [1]; keys [0] = m_ods.tables ["EmployeesList"]; columns ["EmployeeID"]; m_ods.tables ["EmployeesList"]. Primarykey = keys;
The primary key is basically a array of Datacolumn objects.
If you want to use the stored procedure to update your form, or you will use a dedicated non-SQL data provider to operate, then you will often use these command properties.
6.xml's extension support function
In ADO, XML is just as an input and output format. However, in ADO.NET, XML provides you with a series of methods as data logging format, such as: manipulating, reorganizing, sharing, and transferring. No You type Data in DataSet, whether it is not original, it can be processed through a double-sided programming model.
Like the XML document, DataSet reads / writing data and patterns. Data and modes are transferable in HTTP, or run on any support XML platform. The same data can be performed in different schedules through different modes. You use readxmlschema to write mode. The XML mode contains the name of Tables in the Data Set, just like the Relations and Constraints in the Data Set. You should complete this step before calling readxmldata
The following code example is the simplest ASP.NET page that displays the update data table.
<% @ Import namespace = "system.data"%> <% @ import namespace = "system.io"%>