Flylib.com

Books Software

 
 
 

Comparing ADO.NET and ADO


Comparing ADO.NET and ADO

Prior to ADO.NET, ADO was the most popular and powerful Microsoft data access technology. So, how does ADO.NET improve on ADO? To understand the power of ADO.NET, it's not a bad idea to compare ADO.NET with ADO.

The ADO.NET model is inherited from its predecessor ADO, but there are some key differences between ADO and ADO.NET. The following sections describe these differences.

Connections and Disconnected Data

With ADO.NET, you use as few connections as possible and have more disconnected data. In fact, it's recommended to use disconnected data in ADO.NET. In ADO.NET, disconnected data is stored in the form of in-memory DataTable or XML DataSet objects. Having disconnected data not only reduces the load and resource consumption on the server and network, but it also provides better performance.

The ADO.NET DataSet not only supports disconnected data, but it provides rich features in comparison to an ADO recordset. A DataSet , which is a collection of DataTable objects, can store data, but it can also store database table schemas and the relationship between multiple tables.

Recordsets vs. DataSets

In ADO, the in-memory representation of data is the recordset. A recordset is a set of records that can be an entire database table, a section of a table, or a join of more than one table. You use SQL statements to return the set of data. You use a recordset's MoveNext , MovePrevious , MoveFirst , and MoveLast methods to navigate through the data.

Note 

Even though there's no support of move methods in a DataSet, you can implement them using data binding. We discuss data binding in more detail in Chapter 7.

In ADO.NET, the recordset is gone. The new objects introduced in ADO.NET that serve the purpose of the recordset are the DataSet and DataReader . A DataSet object represents a DataSet , and a DataReader object represents a DataReader in ADO.NET. A DataSet is a collection of one or more than one table. A DataSet can also contain metadata of database tables, such as their relationships, constraints, and primary and foreign keys. A DataTable object represents a table of a DataSet . With the help of the DataView object, you can have multiple views of the same DataSet .

A DataSet doesn't have any move methods to navigate through DataSet records like a recordset does. In ADO.NET, a DataTable is a collection of DataRow s. You use a DataRow object to navigate through the records. ADO.NET also provides DataRelation objects to navigate through master and detail records using primary and foreign keys. The DataReader object provides fast-cached, forward-only data to read data and navigate through its records.

XML Support

The ADO.NET model uses XML to store and transfer data among applications, which is not only an industry standard but also provides fast access of data for desktop and distributed applications. ADO uses COM marshaling to transfer data among applications, which is not only hard to program but also not as efficient or reliable as XML because of its limitations, its data types and their conversions, and the nature of COM. Using XML, it's easy to program XML files, and you can also transfer data through firewalls using HTTP, which isn't possible using COM marshaling.



Overview of ADO.NET Namespaces and Classes

The System.Data namespace and its five supporting namespaces define the ADO.NET functionality. In this section, we briefly describe these namespaces. Later chapter describe these namespaces and classes in more detail. These namespaces reside in the System.Data.dll assembly.

Note 

In the .NET Framework, each class belongs to a namespace. A namespace is a logical group of related types and their definitions. To access a type defined in a namespace, you must reference the namespace by using the Imports keyword or by referencing it directly. For example, the System namespace defines all the system-related functionality. For example, the Console class of the System namespace represents the system console. If you want to access the Console class, you need to call System.Console or Imports System in your application.

The six general ADO.NET namespaces are System.Data , System.Data.Common , System.Data.OleDb , System.Data.Odbc , System.Data.SqlClient , and System.Data.SqlTypes . Besides these namespaces, each new data provider may have its own namespace. For example, if you install the Oracle .NET data provider, the installation adds a new namespace, the Microsoft.Data.OracleClient namespace, which also belongs to ADO.NET.

The System.Data namespace is the core namespace of ADO.NET. It consists of the base classes for the ADO.NET architecture. All data providers use these classes. It defines classes that represent tables, columns , rows, and DataSet s. Some common classes from this namespace are DataView , DataViewManager , DataSet , DataTable , DataRow , DataColumn , and DataRelation . To use these classes in your applications, you need to add a reference to the System.Data namespace.

Note 

All classes in the .NET Base Class Library are derived from the Object class and represent a type. Some authors and documentation also use the word type for a .NET Framework class.

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

The System.Data.Common namespace defines common classes. These classes are base classes for concrete data provider classes. All data providers share these classes. DBConnection , DataAdapter , DbDataAdaper , DataColumnMapping , and DataTableMapping are some of the classes defined in this namespace. To use these classes in your application, you need to add a reference to the System.Data.Common namespace in your application. Sql, OleDb, and Odbc.

The System.Data.OleDb namespace defines classes to work with OLE-DB data sources using .NET OleDb data providers. To work with an OLE-DB data source, you must have an OleDb provider for that data source. Each data provider component has a class corresponding to it. These classes start with OleDb , followed by the component. For example, the OleDbConnection class represents a Connection object. Some of the common classes of this namespace are OleDbDataAdapter , OleDbDataReader , OleDbCommand , OleDbCommandBuilder , OleDbError , OleDbParameter , OleDbPermission , and OleDbTransaction . To use these classes in your application, you need to add a reference to the System.Data.OleDb namespace in your application.

Similar to the System.Data.OleDb namespace, the System.Data.Odbc namespaces define Odbc .NET data provider classes to work with the ODBC data sources. To work with ODBC data sources, you need to install an ODBC driver for a database. The System.Data.Odbc namespace classes start with Odbc , followed by the component. For example, the OdbcConnection class represents a Connection object. Some of the common classes of this namespace are OdbcDataAdapter , OdbcDataReader , OdbcCommand , OdbcCommandBuilder , OdbcError , OdbcParameter , OdbcPermission , and OdbcTransaction . To use these classes in your application, you need to add a reference to the System.Data.Odbc namespace in your application.

The System.Data.SqlClient namespaces define Sql .NET data provider classes to work with SQL Server 7.0 or later databases. Similar to Odbc and OleDb classes, the classes in this namespace start with Sql , followed by the component. For example, common classes are SqlConnection , SqlDataAdapter , SqlCommand , SqlDataReader , and SqlTransaction .

The last namespace, System.Data.SqlTypes , provides a group of classes representing the specific types found in SQL Server. Some of these classes are SqlBinary , SqlMoney , SqlString , SqlInt32 , SqlDouble , SqlDateTime , and SqlNumeric .

We discuss these namespaces, classes, and their members in more detail in Chapters 3 and 4.