Professional ASP.NET 1.1 [Electronic resources]

Alex Homeret

نسخه متنی -صفحه : 244/ 67
نمايش فراداده

Introducing Data Management in .NET

Having seen why we need a new data access technology, let's look at what .NET actually provides. In this section, you'll get a high-level overview of all of the .NET data management classes, and see how each of the objects fits with the disconnected and structured programming environment that .NET provides. The remainder of this chapter is divided into two sections; relational data management (techniques such as those you used traditional ADO for) and XML data management (for which, traditionally, you would use an XML parser such as MSXML).

System Namespaces for Data Management

The new relational data management classes are in a series of namespaces based on

System.Data within the class library. The combination of the classes from the namespaces in the following table is generally referred to as ADO.NET:

Namespace

Description

System.Data

Contains the basic objects and public classes used for accessing and storing relational data, such as

DataSet ,

DataTable , and

DataRelation . Each of these is independent of the type of data source, and independent of the connection type.

System.Data. Common

Contains the base classes used by other public classes in the provider- specific namespaces, in particular those used by the classes in the

OleDb ,

Odbc ,

OracleClient , and

SqlClient namespaces. In general, this namespace is not specifically imported into applications.

System.Data. Odbc

Contains the public classes used to connect to and work with a data source via an ODBC driver, such as

OdbcConnection and

OdbcCommand . These objects inherit properties, methods, and events from the base classes in the

Common namespace.

System.Data. OleDb

Contains the public classes used to connect to and work with a data source via an OLE-DB provider, such as

OleDbConnection and

OleDbCommand . These objects inherit properties, methods, and events from the base classes in the

Common namespace.

System.Data.OracleClient

Contains the public classes used to connect to and work with an Oracle database, such as

OracleConnection and

OracleCommand . These objects inherit properties, methods, and events from the base classes in the

Common namespace, and add Oracle-specific features as well.

System.Data. SqlClient

Contains the public classes used to connect to and work with a data source via the TDS interface of Microsoft SQL Server (only), using classes such as

SqlConnection and

SqlCommand . These classes provide better performance by removing some of the intermediate layers required by an OLEDB or ODBC provider. These objects inherit properties, methods, and events from the base classes in the

Common namespace.

System.Data. SqlServerCe

Contains the public classes used to connect to and work with a data source running under Windows CE. These classes are not used or discussed in this book.

System.Data. SqlTypes

Contains public classes to implement the data types normally found in relational databases such as SQL Server, and which are different to the standard .NET data types. Examples are

SqlMoney ,

SqlDateTime , and

SqlBinary . Using these can improve performance and avoid type conversion errors.

There is also a separate series of namespaces containing the classes used to work with XML rather than relational data. These namespaces are based on

System.Xml :

Namespace

Description

System.Xml

Contains the public classes required to create, read, store, write, and manipulate XML documents in line with W3C recommendations. Includes

XmlDocument and a series of classes that represent the various types of node in an XML document.

System.Xml. Schema

Contains the public classes required to create, store, and manipulate XML schemas, and the nodes that they contain.

System.Xml. Serialization

Contains public classes that can be used to convert XML documents to other persistence formats, such as SOAP, for streaming to disk or across the wire.

System.Xml. XPath

Contains the public classes required to implement reading, storing, writing, and querying XML documents using a fast custom XPath- based document. Includes

XPathDocument ,

XPathNavigator , and classes that represent XPath expressions.

System.Xml. Xsl

Contains the public classes required to transform XML into other formats using XSL or XSLT stylesheets. The main object is

XslTransform .

Importing the Required Namespaces

Pages that use objects from the framework's class libraries must import the namespaces containing all the classes that they explicitly create instances of. Many of the common namespaces are imported by default, but this does not include the data management namespaces.

Important

To use any type of data access code, you must import the appropriate namespace.

Importing the System.Data Namespaces

To access relational data, you need at least

System.Data and either

System.Data.OleDb ,

System.Data.SqlClient , or

System.Data.Odbc (depending on the way you're connecting to the data source). In ASP.NET, the

Import page directive is used:

<%@Import Namespace="System.Data" %>

<%@Import Namespace="System.Data.OleDb" %>

Or:

<%@Import Namespace="System.Data" %>

<%@Import Namespace="System.Data.SqlClient" %>

In Visual Basic .NET code inside a class or module, use the

Imports statement:

Imports System.Data

Imports System.Data.OleDb

In C#, use the

using statement:

using System.Data;

using System.Data.OleDb;

At times when you need to specifically import other

System.Data namespaces. For example, to create a new instance of a

DataTableMapping class, you need to import the

System.Data.Common namespace, and to use an SQL-specific data type, you need to import the

System.Data.SqlTypes namespace.

Importing the System.Xml Namespaces

To access XML data using the objects in the framework class library, you can often get away with importing just the basic

System.Xml namespace. However, to create an

XPathDocument instance, you have to import the

System.Xml.XPath namespace as well. To use the

XslTransform class to perform server-side transformations of XML documents, you need to import the

System.Xml.Xsl namespace.

The

System.Xml.Schema namespace is usually only required when working with collections of schemas. Most XML validation objects are in

System.Xml , so you can create an

XmlValidatingReader (for example) without referencing the

System.Xml.Schema namespace. But to create a new

SchemaCollection instance, you must import the

System.Xml.Schema namespace.

Type-Not-Found Compilation Errors

If you forget to import any required namespace, you'll get an error as that shown in Figure 8-3. In this case, it indicates that you have forgotten to import the namespace that contains the class for

OleDbConnection . To solve this particular error, you just need to import the namespace

System.Data.OleDb .

Figure 8-3:

To find out which namespace contains a particular class, you can simply look in the .NET SDK Class Library section within the Reference section, or search for the object/class name using the Index or Search feature of the SDK. Alternatively, use the excellent WinCV (Windows Class Viewer) tool that comes with the .NET installation.

Note

For help on using the tools that come with .NET, check out the SDK section .NET Framework Tools from within the Tools and Debugger section. The WinCV utility is described in detail in the subsection Windows Forms Class Viewer ( Wincv.exe ).