Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать

460 Project 3 ADO.NET IN MANAGED C++

After taking a look at the components of a DataSet object, you will now learn about the types of datasets. Following are the two types of datasets:

Typed datasets

Untyped datasets

The following sections discuss these datasets.

Typed Datasets

Typed datasets are classes that inherit from the DataSet class but are created using

the information stored in XML Schema at design time. An XML Schema (.xsd

 

Y

file) is a document that defines and validates the content and structure of the

L

F

M

 

XML data in a dataset. Therefore, whenever the .xsd file for a dataset is modified, the changes are reflectedAin the typed datasets as well.

A typed dataset inheritsEall the methods, events, and properties of the existing dataset class fromTwhich it derives. In addition, typed datasets incorporate table and column names into IntelliSense in Visual Studio Code Editor. This allows you to access specific tables and columns in the dataset by name. Additionally, the access to tables and columns in a typed dataset is determined at compile time.

Using typed datasets reduces the time taken to access a column or a table. In addition, it facilitates programming by minimizing type mismatch errors and ensures easy readability of the code. You use the typed dataset to perform data manipulation and data binding.

Untyped Datasets

In cases where the XML Schema for a DataSet object is unavailable, unknown, or dynamic, the created datasets are referred to as untyped datasets. Untyped datasets do not have a corresponding XML Schema. In other words, an untyped dataset does not have an inherent structure. Also, an untyped dataset is not a class that you create using a schema, yet, untyped datasets provide you with the functionality of a dataset.

Untyped datasets are generated by creating an instance of the DataSet class in a form or a component. At design time, an untyped dataset does not contain any data or XML Schema. However, at run time, when the dataset is populated with data, XML Schema can be generated for the untyped dataset. You can create the schema for an untyped dataset. It is also possible to use the structure of an

Team-Fly®

INTRODUCTION TO ADO.NET

Chapter 13

461

 

 

 

 

untyped dataset by exporting the structure to a schema file by using the

WriteXmlSchema() method.

An interesting aspect of untyped datasets is that the components of the dataset are available as collections. Therefore, in an untyped dataset, there exist collections, such as collections of DataTable objects and DataColumn objects. This also means that to access a specific dataset component, you need to iterate through the collection of the dataset component or refer to an item by name or index. You can access the collection of DataTable and DataColumn objects in an untyped dataset by using the Tables and Columns properties of the dataset, respectively.

I will now discuss the other component of ADO.NET, the .NET data provider.

.NET Data Providers

As discussed earlier, a .NET data provider is used to establish a connection with the data source, execute commands, and retrieve results. After you work with the results (data) retrieved from the data source, the .NET data provider is again used to update the changes in the data source. Thus, the .NET data provider acts as a bridge between the application and the data source.

A .NET data provider consists of the following four core components:

The Connection object: To transfer data between an application and the database, you first need to connect to the database. ADO.NET provides the Connection object that enables you to establish and manage a connection with the database. The ADO.NET Connection object is similar to the ADO Connection object.

The Command object: After establishing a connection with the database, you can use the Command object for processing requests (in the form of commands) and returning results of these requests from the database. The Command object enables you to perform operations such as updating the records of the database. This object is used when there is a connection with the database. The Command object is quite similar to the ADO

Command object but some new functions have been added to the

ADO.NET Command object.

The DataReader object: The DataReader object is used for reading data in a sequential manner. This object is similar to a read-only, forwardonly recordset and is used for retrieving read-only, forward-only data stream from the database. The DataReader object allows only one row of

462 Project 3 ADO.NET IN MANAGED C++

data to be stored in the memory at any point of time. This results in more efficient performance of the application and reduction in the system overheads.

The DataAdapter object: The DataAdapter object is used for transferring data from the data source to the dataset and vice versa. You work with data in the dataset and then transfer the changed data back to the data source.

So, the DataAdapter object is used for communication between the data source and the dataset. The DataAdapter object can transfer data not only between a dataset and a database but also between a dataset and some other sources, such as the Microsoft Exchange Server.

The DataAdapter class consists of a set of properties that facilitate loading and updating data. Table 13-2 lists some DataAdapter properties.

Table 13-2 DataAdapter Properties

Property

Description

SelectCommand

Returns or sets a T-SQL statement or stored procedure used to

 

select records from a data source.

UpdateCommand

Returns or sets a T-SQL statement or stored procedure used to

 

update records in a data source.

DeleteCommand

Returns or sets a T-SQL statement or stored procedure used to

 

delete records from a dataset.

InsertCommand

Returns or sets a T-SQL statement or stored procedure used to

 

insert records in a data source.

 

 

Figure 13-4 illustrates the interaction between the .NET data provider objects.

After having discussed the various core components of a .NET data provider, you will now revisit the types of .NET data providers.

ADO.NET provides the following types of .NET data providers:

The OLE DB .NET data provider

The SQL Server .NET Data Provider

ODBC .NET data provider

You use the ODBC .NET data provider to access ODBC-compliant data sources. The following sections discuss the first two .NET data providers in detail. The

INTRODUCTION TO ADO.NET

Chapter 13

463

 

 

 

 

Connection object

Database

CommandObject

DataReaderObject

DataAdapterObject

FIGURE 13-4 Interaction between the .NET data provider objects

implementation of the ODBC .NET data provider is similar to the other two

.NET data providers.

In addition to the .NET data providers provided by ADO.NET, you can create and implement your own .NET data providers. Figure 13-5 illustrates the different components and types of .NET data providers.

The OLE DB .NET Data Provider

The OLE DB .NET data provider enables you to access data by using COM interoperability. It works with several OLE DB providers, such as SQLOLEDB (the SQL OLE DB provider), MSDAORA (the Oracle OLE DB provider), and Microsoft.Jet.OLEDB.4.0 (the Jet OLE DB provider). However, it does not provide support for MSDASQL, the OLE DB provider for ODBC.

As discussed earlier, the classes of the OLE DB .NET data provider are stored in the System.Data.OleDb namespace. Some of the most common classes for the OLE DB .NET data provider are described in Table 13-3.

464 Project 3 ADO.NET IN MANAGED C++

.NET data providers

 

 

 

 

 

 

 

 

 

 

Objects

 

 

 

 

Types

 

 

 

 

 

 

 

 

 

 

 

 

Connection

 

 

SQL server

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Command

 

 

 

 

 

 

 

 

 

 

 

 

OLE DB

 

 

 

 

 

 

 

 

DataReader

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DataAdapter

 

 

ODBC

 

 

 

 

 

 

 

 

 

 

 

 

FIGURE 13-5 Components and types of .NET data providers

Table 13-3 Classes for OLE DB .NET Data Provider

Class

Description

OleDbConnection

Represents an open connection with the data source.

OleDbCommand

Represents a SQL statement or stored procedure for execution

 

against the data source.

OleDbDataReader

Provides a means to read data rows from the data source in a for-

 

ward-only mode. Has similarity to the read-only, forward-only

 

recordset of ADO.

OleDbDataAdapter

Represents the data commands and database connections used for

 

transfer of data from the data source to the dataset and vice versa.

OleDbError

Compiles information pertaining to errors or warnings that the

 

data source returns.

 

INTRODUCTION TO ADO.NET

Chapter 13

465

 

 

 

 

 

 

 

 

 

 

 

 

Class

Description

 

 

 

 

 

 

 

 

 

OleDbException

Represents the exception that results when the OLE DB data

 

source returns an error or warning.

 

 

 

 

OleDbPermission

Enables the OLE DB .NET data provider to verify whether a

 

user has enough security permissions to acquire access to the

 

OLE DB data source.

 

 

 

 

OleDbTransaction

Represents a SQL transaction for the data source.

 

 

 

 

 

 

 

 

 

 

The SQL Server .NET Data Provider

The SQL Server .NET data provider enables access specifically to Microsoft SQL Server databases by using the Tabular Data System (TDS) protocol. For this purpose, the SQL Server .NET data provider does not require the use of any OLE DB provider through COM interoperability. To be able to use the SQL Server .NET data provider, you need to have access to Microsoft SQL Server 7.0 or later.

As discussed earlier, the classes of the SQL Server .NET data provider are stored in the System.Data.SqlClient namespace. Some of the most commonly used classes for SQL Server .NET data provider are described in Table 13-4.

Table 13-4 Classes for SQL Server .NET Data Provider

Class

Description

SqlConnection

Represents an open connection with a SQL Server database.

SqlCommand

Represents a Transact-SQL statement or stored procedure for

 

execution against the Microsoft SQL Server database.

SqlDataReader

Provides a means to read data rows from the Microsoft SQL

 

Server database in a forward-only mode.

SqlDataAdapter

Represents the data commands used for transfer of data from the

 

Microsoft SQL Server database to the dataset and vice versa.

SqlError

Compiles information pertaining to errors or warnings that the

 

Microsoft SQL Server database returns.

SqlException

Represents the exception that results when the Microsoft SQL

 

Server returns an error or warning.

SqlTransaction

Represents a Transact-SQL transaction for the Microsoft SQL

 

Server database.

 

 

466 Project 3 ADO.NET IN MANAGED C++

Table 13-5, 13-6, 13-7, and 13-8 describe the methods of the classes SqlConnec-

tion, SqlCommand, SqlDataReader, and SqlDataAdapter, respectively.

Table 13-5 SqlConnection Methods

Method Description

ChangeDatabase

Changes the current database to use another database for an exist-

 

ing SqlConnection.

Close

Closes the connection with a database.

CreateCommand

Creates and returns an SqlCommand object for a particular

 

SqlConnection.

 

Table 13-6 SqlCommand Methods

 

 

Method

Description

 

 

ExecuteReader

Returns a DataReader object by invoking commands by using

 

the T-SQL sp_executesql system stored procedure.

ExecuteNonQuery

Executes commands that do not return rows, such as T-SQL

 

INSERT, DELETE, UPDATE, and SET statements.

ExecuteScalar

Retrieves the value of the first column of the first row in the

 

result set obtained after executing a query.

 

Table 13-7 SqlDataReader Methods

 

 

Method

Description

 

 

Read

Accesses the current row of the result set retrieved from a data

 

source and moves SqlDataReader forward to the next row.

NextResult

Navigates through result sets in a sequential manner. Applicable

 

only when DataReader consists of multiple result sets.

GetSchemaTable

Retrieves the schema-related information about the current result

 

set in the form of a DataTable object.

GetDateTime

Returns the column value as a DateTime object.

GetInt32

Returns the column value as a 32-bit signed integer.

Close

Closes the current DataReader object.

 

 

 

INTRODUCTION TO ADO.NET

Chapter 13

467

Table 13-8 SqlDataAdapter Methods

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Method

Description

 

 

 

 

 

 

 

 

 

Fill

Populates or refreshes data in a dataset and uses the same data

 

structure in the dataset as in the data source.

 

 

 

 

Update

Modifies a data source to reflect changes in the data of a dataset

 

by using the appropriate T-SQL statements, such as INSERT,

 

UPDATE, or DELETE statements, against the data source.

 

 

 

 

 

 

After having discussed the types of .NET data providers, you might be wondering how to decide the type of data provider for your application. The .NET data provider you need to use is determined by the nature of the application and the data source that is to be accessed. The recommended implementations for .NET data providers are as follows:

SQL Server .NET data provider: This data provider is recommended in case of middle-tier applications using SQL Server 7.0 or later and sin- gle-tier applications using Microsoft Data engine (MSDE) or SQL Server 7.0 or later.

OLE DB .NET data provider: This data provider is recommended in case of middle-tier applications using Oracle or SQL Server 6.5 or earlier and single-tier applications using the MS Access database.

ADO.NET and XML

An important feature of ADO.NET is its built-in support for XML. ADO.NET was designed along with the .NET XML Framework. Both of them are components of the same architecture. ADO.NET provides both implicit and explicit support for XML, as compared to ADO, which does not inherently support XML but provides only an explicit XML support. ADO.NET uses XML internally as the format for storing and transferring data. This is a major advantage over ADO. You need not know XML to work with ADO.NET. This is because you are not exposed to the process of converting data into and from XML.

To understand the reason for choosing XML with ADO.NET, you need to know the advantages of using XML. An important advantage of XML is that it is an industry standard format. XML facilitates data exchange between components of one application with any other data component of another application. In

468 Project 3 ADO.NET IN MANAGED C++

addition, XML is a text-based format. This implies that XML can be sent through any protocol, such as HTTP. The exclusion of any binary information in XML also facilitates easy communication across networks by bypassing firewalls.

Extensive support for XML was built in ADO.NET keeping the advantages of XML in mind. For instance, the two-way exchange of data between a dataset and a data source is performed using XML. Similarly, data exchange between the components of the ADO.NET architecture occurs through XML. To facilitate data exchange between the components of the ADO.NET architecture, the contents of DataSet are converted to XML files or streams by data APIs. This XML format is then sent to another component where data APIs can be used to convert the XML format to a DataSet object. In addition, you can populate a dataset with data from an XML source, such as an XML file or an XML stream of data.

Summary

In this chapter, you were first introduced to the various data access techniques: DAO, ODBC, RDO, OLE DB, and ADO. You also learned about the advantages and disadvantages of these techniques and about their evolution. Next, you were introduced to ADO.NET and its features and advantages. You learned that ADO.NET supports disconnected data architecture, provides datasets, and supports XML. ADO.NET makes applications scalable by enabling them to effectively manage multiple users. ADO.NET applications also provide better performance and are easily programmable. ADO.NET enables you to create applications that are interoperable with other applications as well as easy to maintain.

Next, you were introduced to ADO.NET architecture. You first learned about data-related namespaces and then about the two main components of ADO.NET, datasets and .NET data providers. You learned about the two types of .NET data providers, the OLE DB .NET data provider and the SQL Server

.NET data provider. Finally, you learned about the ADO.NET support for XML. ADO.NET provides implicit as well as explicit support for XML. XML is integrated with ADO.NET in the form of the DataSet.

In the next chapter, you will learn to implement ADO.NET in a Managed Extensions application.

Chapter 14

Implementing

ADO.NET in a

Managed C++

Application