ADO.NET enables datacentric applications to connect to various data sources and retrieve, manipulate, and update data. ADO.NET uses XML to transfer data across applications and data sources. This enables you to use ADO.NET to access data from data sources that expose data via OLE DB or ODBC or from the data sources that providers are available for. The .NET Framework includes SQL Server .NET Data Provider to access Microsoft SQL Server (version 7.0 and later) and OLE DB .NET Data Provider to access database servers that use OLE DB to expose data. In addition, you can also download ODBC .NET Data Provider and Oracle .NET Data Provider from http://msdn.microsoft.com/downloads. In traditional database applications, clients establish a connection to a database and keep the connection open until the application completes execution. Open database connections require system resources. For example, multiple open connections make the database server slow to respond to client calls because most databases can only maintain a small number of concurrent connections. Similarly, applications that require an open database connection are difficult to scale.
One of the advantages of ADO.NET is that it supports disconnected architecture. Using a disconnected architecture, applications connect to a database server only to retrieve or update the data. This enables you to reduce the number of open connections to various database servers. ADO.NET also provides a common data representation that enables you to access data from multiple and various types of data sources and have it appear as one entity.
ADO.NET allows you to use data commands to execute SQL statements or stored procedures from a client application. These data commands enable you to execute SQL statements on a database server easily and quickly. For example, to retrieve a set of rows from a database, you establish a connection to the database, create a data command, specify the SQL SELECT statement to retrieve the required records, and call the execute method of the command. The command object returns the set of rows, which you can process immediately or store in an ADO.NET DataSet for processing at a later time.
An ADO.NET DataSet is a cache of records that you retrieve from a data source, such as a database or an XML file. A DataSet contains records from one or more tables. In addition, a DataSet contains information regarding the relationships between tables. DataSets enable you to process data whenever a user needs to access and process data. You can also use DataSets without a data source to manage data from an application or from an XML file. In addition, DataSets enable you to remain disconnected from the database. Another advantage of DataSets is that components can exchange DataSets. For example, a business object in the middle tier can create and populate a DataSet and then pass it to another component in the application, which then processes the DataSet.
When you create a datacentric application using ADO.NET, data moves between various objects. The data first moves from a data source to a DataSet and then to components, such as controls, in a form. ADO.NET uses XML to transfer data between various components of an application. The ADO.NET data APIs auto-matically create the XML files of the data in a DataSet and send them to other ?components.
The application you create accesses a data source by using a .NET data provider. A .NET data provider enables you to connect to a data source and execute commands to retrieve results and manipulate data. The .NET Framework provides two data providers: OLE DB .NET and SQL Server .NET. You use the OLE DB .NET data provider to connect to and access an OLE DB data source, whereas the SQL Server .NET data provider enables you to connect to and access a SQL Server database. These data providers are an important part of the ADO.NET architecture.
The classes of ADO.NET are defined in the System.Data namespace. This namespace defines classes, such as DataSet and DataTable, which constitute the ADO.NET architecture. Therefore, you add a reference to the System.Data name-space in your application when you want to use ADO.NET. The next section discusses the ADO.NET architecture.
ADO.NET enables data transfer between components, such as data sources, DataSets, and the applications that request data. Together, all of these components constitute the ADO.NET architecture. Figure 5.1 displays the ADO.NET ?architecture.
In addition to the data source, the ADO.NET architecture includes data providers and DataSets.
A data source is a database server for which the .NET Framework provides a data provider or an XML file. To access a data source, you create a connection to the data source using the data providers of ADO.NET. The ADO.NET data providers enable you to establish a connection with a data source and perform other tasks, such as executing SQL commands on data sources. You will learn more about data providers later in this lesson.
The datacentric applications that you create usually need to access data from multiple tables. In addition, you might need to process data from multiple tables as one entity. For example, an organization might need to access the names of all its customers and the quantity of each product supplied to them.
ADO.NET DataSets are designed to store data in disconnected and distributed data environments. ADO.NET DataSets enable you to store data from multiple data sources. The DataSet stores data in a collection of tables. In addition, DataSets store relationships between tables. A DataSet is the data source for the application that requests data. The application can access and manipulate records in the DataSet without having to repeatedly connect to the database.
The functionality of DataSets is defined in the DataSet class. The DataSet class consists of a collection of one or more DataTable objects that represent tables. You will learn about DataSets in Lesson 3.
.NET data providers enable an application to connect to a data source, execute commands, and retrieve results. A .NET data provider consists of the Connection, Command, DataReader, and DataAdapter objects, which you use to perform such tasks as connecting to a database and executing SQL commands. Table 5.1 describes the function of each object.
| Object | Description |
| Connection | This object enables you to establish and manage a connection to a database. |
| Command | This object enables you to execute SQL commands and retrieve results from a database. The Command object also enables you to perform other tasks, such as updating the records of the database. |
| DataReader | This object enables you to read data in a sequential manner. The DataReader retrieves a read-only, forward-only data stream from the database. However, the DataReader object allows you to store only one row of data in memory at any point in time. |
| DataAdapter | This object enables a database and a DataSet to communicate with each other. You use the DataAdapter object to transfer data between a data source and a DataSet. In addition, the DataAdapter object can transfer data between a DataSet and some other applications, such as Microsoft Exchange Server. |
Visual Studio .NET and the .NET Framework include two ADO.NET data providers: an OLE DB .NET data provider and a SQL Server .NET data provider.
The OLE DB .NET data provider enables you to connect to the OLE DB data sources, whereas the SQL Server .NET data provider enables you to connect to SQL Server 7.0 and later databases. The System.Data namespace contains two namespaces: System.Data.OleDb and System.Data.SqlClient. These namespaces contain classes for the OLE DB .NET and SQL Server .NET data providers, respectively. Therefore, to use a data provider in your application, you add a reference to the appropriate namespace. The classes of each data provider contain methods that enable you to perform the following tasks:
-
Create a connection with a database
-
Execute SQL statements or stored procedures on a database
-
Read data rows from a database in forward-only mode
-
Transfer data between a database and a DataSet
-
Display errors and warning messages returned by a database
-
Handle exceptions when a database returns an error or warning
-
Execute Transact-SQL statements on a database
XML is an important component of the ADO.NET architecture. ADO.NET uses XML internally to store and transfer data. You need not explicitly convert data to the XML format or the XML format to data. XML is integrated with ADO.NET as DataSets. The structure of the DataSet, including table definitions, columns, data types, and constraints, is defined by using an XML schema. You can serialize the data within a DataSet as XML. Similarly, you can serialize the structure of the DataSet as an XML schema.
The components of the ADO.NET architecture listed in Table 5.1 enable you to access data and perform operations on data sources easily.
ADO.NET provides many benefits that will help you create datacentric applications. The following sections discuss the benefits of ADO.NET.
Interoperability is one of the key benefits provided by ADO.NET. Because ADO.NET uses XML to exchange data, any component that understands XML can receive data from ADO.NET. For example, you can transfer data between ADO.NET and an application that is running on any platform. The integration of XML and ADO.NET allows ADO.NET to operate easily with the applications that support XML.
Applications created using ADO.NET are easier to manage and scale than applications created using ADO. For example, after developing an application, you might need to change its architecture to improve its speed or increase the number of people who can access the application. Consider the example of an e-commerce site. As the e-commerce site becomes popular, the number of hits on the site increases. If the number of hits increases, you might need to change the architecture of the application and increase the number of tiers. However, increasing the number of tiers in a deployed application is a difficult and time-consuming task. In addition, problems might occur during data exchange or data transfer between the tiers. ADO.NET enables you to easily increase tiers in a deployed application because ADO.NET uses XML to transfer data between tiers. This enables the objects in new tiers to exchange data seamlessly.
ADO.NET simplifies programming for various tasks, such as executing SQL commands. This enables you to increase productivity and minimize the number of errors. For example, you can use the ADO.NET data commands to execute SQL statements or stored procedures. The actual task of building and executing a SQL statement is abstract and performed by ADO.NET. In addition, the ADO.NET data classes enable you to use typed programming to write code. Typed programming allows automatic statement completion. Therefore, it becomes easy to write code. In addition, typed programming increases the safety of the code and reduces the number of compilation errors.
ADO disconnected recordsets use COM marshaling to transfer data between applications. This requires data type conversion in order for COM to recognize the data types, and the conversion diminishes the performance of an application. Alternatively, ADO.NET uses XML to transfer data. Therefore, the requirement to convert the data type does not exist, which increases the performance of an application.
With the increase in data and the change in the business models of organizations, the demand for data has increased rapidly. Consider a Web site that sells sporting goods. When a prospective buyer wants to view product information, the information is available in the Products database. If several prospective customers accessing the Web site simultaneously want to view information about the same product, the demand for data from the Products database increases. ADO.NET enables your applications to scale according to requirements because it uses disconnected architecture. This enables you to reduce the open connections to the database and results in optimum usage of resources.
The features of ADO.NET discussed in the preceding sections provide greater benefits than ADO. The following section explains the basic differences between ADO and ADO.NET.
ADO and ADO.NET have various differences, such as differences in architecture, data representation, and methods of sharing data between applications.
ADO uses a recordset to represent data that is retrieved from tables in memory, whereas ADO.NET uses DataSets. A recordset usually contains data from a single table. To store data from multiple tables, you use a JOIN query. The JOIN query retrieves the data from multiple tables as a single result table. Alternatively, ADO.NET uses a DataSet to represent data in memory. As mentioned earlier, a DataSet can store data from multiple tables and multiple sources. In addition, a DataSet can also contain relationships between tables and the constraints on a table. Therefore, a DataSet can represent the structure of a database.
ADO provides a read-only navigation on recordsets, which allows you to navigate sequentially through the rows of the recordset. However, in ADO.NET, rows are represented as collections. Therefore, you can access records using the primary key index. In addition, you can also filter and sort results.
In ADO.NET, you only connect to a database to retrieve and update records. You can retrieve records from a database, copy them into a DataSet, and then disconnect from the database. Although a recordset can provide disconnected data access in ADO, ADO was primarily designed for connected scenarios.
In ADO.NET, you communicate with the database using a DataAdapter or a DataReader that makes calls to an OLE DB provider or to the APIs provided by the data source.
You use COM marshaling in ADO to transfer a disconnected recordset from one component to another. In ADO.NET, you transfer a DataSet using an XML stream. XML provides the following advantages over COM marshaling when transferring data:
- Richer data types.
COM marshaling can only convert data types that are defined by the COM standard. In an XML-based data transfer, restrictions on data types do not exist. You can use XML-based data transfer to transfer any data that is serializable.
- Bypassing firewalls.
A firewall does not allow system-level requests, such as COM marshaling. Therefore, a recordset cannot bypass a firewall. However, because firewalls allow HTML text to pass and ADO.NET uses XML to transfer DataSets, you can send an ADO.NET DataSet through a firewall.

No comments:
Post a Comment