1. Trang chủ
  2. » Công Nghệ Thông Tin

ado.net

61 97 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề ADO.Net
Trường học Sample University
Chuyên ngành Computer Science
Thể loại Lecture Notes
Năm xuất bản 2023
Thành phố Sample City
Định dạng
Số trang 61
Dung lượng 1,2 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

What You Will Learn • The difference between ADO and ADO.NET • The benefits of ADO.NET • ADO.NET core concepts and architecture, including the ADO.NET object model, introduction to the S

Trang 1

ADO.NET

Trang 2

Objectives

This module introduces ADO.NET, the evolutionary next step for Microsoft® ActiveX® Data Objects (ADO)

What You Will Learn

• The difference between ADO and ADO.NET

• The benefits of ADO.NET

• ADO.NET core concepts and architecture, including the ADO.NET object model, introduction to the

System.Data namespace, the DataSet, and data

views; NET data providers, and more

Recommended Reading

• Microsoft ADO.NET newsgroup:

microsoft.public.dotnet.framework.adonet

Trang 3

ADO.NET 3

Overview

ADO.NET and the NET Framework

ADO.NET is part of the Microsoft NET Framework If you break down the Framework into sections (common language runtime, base classes, data and XML, Web Services and user interface), then ADO.NET sits squarely

in the data and XML section

Trang 4

Designed for connected access

ADO is based on the concept of a 24/7 (24 hours, 7 days

a week) “connected world,” such as is found on a corporate local area network (LAN) You create a

RecordSet; connect it to a data source—most often a

database—and work The RecordSet stays “plugged in,”

if you will, to the data source, and changes to the data are posted to the data store immediately

A model based on connected data can make it difficult and impractical to exchange data across application and organizational boundaries If two components need to share the same data, both have to be connected, or a way must be devised for the components to pass data back and forth

There are times when it is still useful to work with connected data For example, in an application that performs a high volume of updates with possible record contention, a connected data architecture can be very important A typical scenario is a ticket-booking application, where users need to work with information that is up to the moment For these types of applications,

you might wish to design your data access around ADO

Trang 5

RecordSet is one table that contains all the data

The ADO RecordSet is limited in flexibility and

functionality For example, most useful data analysis or presentation requires views of your data that span multiple tables or data sources Using ADO, this cannot

be accomplished without performing a SQL JOIN As you may or may not know, this is a performance drag It consumes memory and CPU power on the database server—precious resources especially with today’s Internet user demands

Because a RecordSet is essentially a single result table,

navigation is sequential, row-by-row Thus, if you perform

a joining query the resulting data is “flattened”; any relations that may have existed are not relationally navigable

Another, perhaps more impressive point is that ADO does not support an abstract data model It is tied to the physical data source

Data types are bound to COM/COM+ data types

A rather significant limitation of ADO is that the available data types are restricted to Component Object Model (COM) and COM+ data types That means that sometimes you need to fit a square peg in a round hole For example, in COM/COM+ programming the BSTR is typically used to represent strings that need to be interoperable across languages For those of you who do not know, the BSTR type is a null-terminated string whose leading WORD contains the length of the string Unfortunately, the BSTR type is really a stranger to all languages and only makes sense in the COM context For

C, C++, and other lower level languages you must use special COM run-time functions to create and destroy them, and rapid application development (RAD) environments like Microsoft Visual Basic® need explicit support in the runtime to handle these types

Data sharing via COM marshalling

Sharing data between components in your application or elsewhere is done through COM marshalling This limits sharing of data to a COM or COM-friendly environment

Trang 6

Problems marshalling through firewalls (DCOM, binary)

There are also problems with marshalling through firewalls, because they tend to restrict the type of data that can pass through them COM marshalling requires (COM) system services on the “other side” of the firewall (the server), but firewalls are often set up to block such traffic because it could pose a security threat

Trang 7

ADO.NET 7

Overview

ADO vs ADO.NET (2/2)

ADO.NET Designed for disconnected access

In contrast to ADO, ADO.NET, like .NET in general, is designed with Internet technology, although not limited to creating Internet-based solutions As such, the access model of ADO.NET is a disconnected one

Can model data logically!

An additional improvement over ADO is that ADO.NET provides the means to model your data abstractly This is

achieved through the successor of the RecordSet: the

DataSet

DataSet can contain multiple tables

The DataSet can model data logically or abstractly

because, unlike the RecordSet, the DataSet is not a data

container capable only of stuffing rows of data into itself You now have the ability to create an in-memory data schema that includes multiple tables and the relationships between them!

Two intended results of this are that relational navigation

is now possible and you can create views on multiple tables or data sources without performing a database JOIN, but more on this later

Data types are only bound to XML schema—No data type conversions required

Considering the flexibility the DataSet affords, it should

not surprise you that ADO.NET gives you the ability to use any data type This is accomplished through its use of XML schemata If you can describe it in the schema, you can use it—and avoid data conversions!

Trang 8

XML, like HTML, is plaintext: “Firewall friendly”

The use of XML and XML schemata in ADO.NET also

makes it possible to share data between DataSets and

other components or applications—without limitation Because XML is just plaintext it can more readily pass through firewalls without being burned The reason is that, unlike binary data (COM/COM+), firewalls can inspect XML data —“it's just text.”

Trang 9

Interoperability through use of XML

XML is an open Web standard, is human readable and decipherable text, and is data that describes itself (metadata) This makes it easy to share, interpret, and work with For these reasons, XML is used to represent and transfer all data in ADO.NET

Scalability through the disconnected DataSet

On the scalability side of things, because connections are not maintained for long periods, there is little possibility that database locking will occur However, by specifically using Service Components you can perform locking ADO.NET brings the connected world of the Internet to your solutions through its disconnected model You can now design and implement your applications without the extra consideration toward resource loads on your database-server tier Simply: Connect, execute one or more queries, and disconnect

Maintainability

ADO.NET, like much of NET, is built around the idea of separation of data logic and user interface This means you can create your application in independent layers For example, you can establish a data access layer, a business logic layer, and an interface façade (or user interface) Designing and building your application this way limits “collateral damage” when you update any given aspect of your solution

Trang 10

Visual Studio.NET Enhancements

Typed programming—a programming style

Typed programming is a programming style in which user words are used to construct statements or evaluate expressions

end-For example, if you wish to select the “Balance” column for “Jones” from the “Customer” table it is certainly more intuitive to do so by writing

To this end, ADO.NET enables you to create typed

DataSets in which you define the schema for the DataSet

and its data Microsoft Visual Studio.NET dynamically creates code based on a schema (.xsd) and generates a

file (.cs) containing the strongly typed DataSet Visual

Studio.NET uses this for statement completion, as well as compile-time type checking

Wizard Support

Visual Studio.NET provides several “wizards” aimed to make rapid application development possible These wizards enable you to visually select the tables you wish

to work with from your data source, for instance, and then automatically generate the select, insert, update and delete queries for you! The wizards also make creaing DataSet objects based on your selection of tables automatic – by selecting “Generate DataSet” the appropriate object is generated

Trang 11

ADO.NET 11

There is also an XML Designer that makes it easy to create data schemas By using the designer in this way

you create the DataSet and its corresponding DataTable

objects and relations There is plenty of additional wizard support for ADO.NET in Visual Studio.NET, but that it not the topic of this module

Trang 12

Core Concepts and Architecture

Core Concepts and Architecture

The ADO.NET Object Model

The ADO.NET object model is comprised of several

namespaces, but in sum it is: DataSet and its related

objects and the NET data providers

The core namespaces of ADO.NET that you will work with

are System.Data and System.Data.OleDb The

System.Data.Common namespace contains the classes

that are common to all NET data providers

Two other namespaces that are arguably part of

ADO.NET are System.Data.SqlClient and System.Data

SQLTypes; these are both optimized for use with

Microsoft SQL Server™

Combined, these five namespaces provide you with the means to connect to data sources, retrieve information, store it (in an in-memory cache), update your cached data, and post it back to the data sources You also have the ability to modify the data source’s schema from your

DataSet That is, you can create new tables, make new

relations, and update the data source with the information

Trang 13

ADO.NET 13

Core Concepts and Architecture

ADO.NET-related Namespaces

You may think of ADO.NET as a pyramid in which the

top-most, main tier is the System.Data namespace Below

that you have the System.Data.Common,

System.Data.OleDb, System.Data.SqlClient, and

System.Data.SqlClientTypes namespaces

Trang 14

Core Concepts and Architecture

System.Data Namespace

The ADO.NET object model is contained in the System.Data namespace

Contains the basis and bulk of ADO.NET

As its name implies, System.Data is “all about data”! For this reason, System.Data is the centerpiece of ADO.NET

and the data-centric namespace

Provides the means to work on and with your data!

• It contains a myriad of classes for working with data

• Ability to create views of your data

• Means to logically represent your data

• Enables the use of XML to view, share, and store data

It is the means to work on and with your data!

Trang 15

ADO.NET 15

Core Concepts and Architecture

Introducing the Objects…

Following are a few of the objects that are “first class

citizens” in the System.Data namespace You will find

yourself working with these classes regularly

Used to relate two DataTables to each other As you

will learn, this is similar to how a relation is created in a SQL database

DataViewManager

Used to create one or more views (using DataView objects) on the DataTable objects of a DataSet

Trang 16

Core Concepts and Architecture

Putting the Objects Together…

This diagram shows you how some of the classes in the

System.Data namespace fit together with relation to the

all-important DataSet This diagram does not show which objects a DataSet must contain but what it can—or more

accurately, most likely will—contain It should be clear to

you from this diagram that the DataSet is the ADO.NET

view of data

In this example we have a DataSet in which three

DataTables have been added and two DataRelations

Notice that the DataTables are contained by the DataSet

Tables container and the DataRelations are contained by

the Relations container

You may add the appropriate items to each of these

containers using their Add methods Access to members

of the containers is achieved using array-style indexing

Note that the lines connecting the DataTables to the

DataRelations are meant to show that a relation between

the DataTables has been specified You may do this by

specifying that one or more columns in two different tables

are related Note that the DataColumn objects of each of

the tables you wish to relate must be of the same data type

The DataView and DataViewManager objects shown are

meant to convey that one or more views may be created

on any given DataTable

Trang 17

ADO.NET 17

Core Concepts and Architecture

Working the Data – The DataSet

Now that we have an overview of the DataSet and understand that it acts as the main component of System

Data, let us explore a little deeper into the DataSet.

An in-memory cache of data from a data source

A DataSet is an in-memory cache of data loaded from

your data source or sources using one or more query commands Because it is in memory, manipulation and viewing of the data is obviously fast

Common way to represent and manipulate data

The real advantage that the ADO.NET DataSet provides

is that it is a common way to represent and manipulate data In other words, it is a “universal data container.” You may ask what “universal” means Well, it is universal

because it is designed to contain data from any data

source That source might as easily prove to be a component in your application as a database When you

think DataSet, think “set of data” not “cache of data from

the database"! Taken in this context, you should see the

potential of using DataSets throughout your application

solution—not only when querying a database but any time you need to manipulate or move data around

Logical representation of data

A DataSet is a logical representation of data depending

on the query/result set The DataSet does not restrict you

to mapping directly to the physical data source layout Alternatively, you may create your own logical representation of the data

For example, assume your database has three tables: Customers, Addresses, and CreditCards You can

Trang 18

perform a query on your for customers whose addresses are in a particular region and also retrieve their credit card information The result of your query would be stored in

the DataSet, but the corresponding Customers,

Addresses, and CreditCards tables need not exist in the

DataSet

On the other hand, you can create tables in the DataSet, called DataTables, and create relations between them Again, the tables and relations you create in the DataSet

need not be bound to those in the data source but may be your own logical representation

Disconnected from the data source

The DataSet is an in-memory cache designed to be

disconnected from the data source You simply connect to

the data source using an OleDbConnection object (or

SqlConnection object), execute your query or queries,

and then close the connection You can then work with the cached data, and when you are ready to update the data source you simply reconnect to the data source and issue an update command We have already learned some of the advantages of this disconnected philosophy

XML used to read and write data and schema

In the .NET Framework, XML plays an important and pervasive role So, why should ADO.NET be any

different? As we have already learned, DataSet objects

communicate using XML documents, which means they can communicate with just about anything!

You will learn more about the reading and writing XML documents in ADO.NET later At this time, suffice it to say

that the DataSet stores and communicates using XML

and XML schemata

Trang 19

ADO.NET 19

Core Concepts and Architecture

Properties & Methods of Interest

Collections are used to add and remove tables and relations

Before we discuss the properties of the DataSet, let us

first talk about properties in general with a focus on those that return collections

When a property returns (or “gets”) a collection of items, the general rule is that the collection’s type name is the property name appended with “Collection.” In the case of

the Tables property, for instance, the collection is of type

DataTableCollection You may then iterate over the

collection to get each object it contains You may also use

a collection’s Add() or Remove() methods to add or

This property is used to retrieve the

DataTableCollection, the collection used to hold any DataTable objects added to the DataSet.

Relations

Returns the RelationsCollection of DataRelation

objects, representing logical relationships between

tables in the DataSet.

Namespace

Get or sets the namespace associated with the

DataSet This is used for scoping elements and data,

which is especially useful when reading or writing a

DataSet’s XML schema or data

Trang 20

Using Properties Sample

The following code snippet uses the Tables property of the DataSet myDataSet to add a newly created

DataTable to the DataSet.

DataTable newTable = new DataTable(

In the following example, the DataTableCollection of a

DataSet is returned so that you may iterate over it to

retrieve the DataTables that are contained in the

DataSet.

DataTableCollection myDataTableCollection; myDataTableCollection = myDataSet.Tables;

// Iterate over the collection

// Iteration not shown.

Trang 21

ADO.NET 21

Core Concepts and Architecture

All About Data…

Simply to reiterate a very important point: a DataSet is a Universal Data Container—It’s not just for databases!

Trang 22

Core Concepts and Architecture

The DataTable

Maps DataSet data to a physical table in the data source

You may create a DataTable object and map it to a data

source’s table That is, if your data schema contains a

table called Customers you can create a DataTable in the

DataSet to correspond to the physical table in a

one-to-one manner It is also possible to create DataTable

objects that do not correspond to any table in the data source The choice is yours, depending solely on what makes sense logically to you

Can be related to one another through DataRelations

In a physical model of the database or other data schema, tables are often related to one another in different logical ways For example, a CustID column in one table may have a corresponding column in another table An example would be a Customers table and an Addresses table (for customers) As a customer may have more than one address, it is logical that a one-to-many correspondence may exist via a common column, such as the customer name or ID

ADO.NET facilitates creating this type of relation between

DataTable objects with DataRelation objects

Properties of Interest

Columns

Gets the columns as a collection This returns the

ColumnsCollection containing any/all DataColumn

objects

Returns the rows of the DataTable Technically, a

RowsCollection of DataRow objects is returned.

Trang 23

ADO.NET 23

ParentRelations

Gets the parent relations as a RelationsCollection

These are the logical relationships of one table to other tables

Gets or sets the primary key of the table using an array

of DataColumn objects The contained combined

objects comprise the table’s primary key

Trang 24

Core Concepts and Architecture

System.Data—DataSet and DataTable

Create a DataTable using a DataSet.

1 Create a DataColumn, providing the column name

and data type

2 Add the DataColumn to the DataTable.

3 Use the DataSet.Tables property to add the

DataTable to the DataSet

4 Repeat until satisfied!

Trang 25

ADO.NET 25

Core Concepts and Architecture

Relating Data – The DataRelation

Having already learned something about the

DataRelation when you learned about the DataTable and DataSet objects, it is time to expand on DataRelation

objects

Used to relate two DataTable objects to each other

The DataRelation object uses DataColumn objects to create the relation between two DataTables.

If you have any database experience, it will be obvious to

you that the DataType (the type of data) for both columns

must be the same For example, one column cannot be of the integer type while the related column is of the character type

A nice feature of DataRelations in ADO.NET is that you

can name the relation anything you like, making it easier

to remember, and use this name to refer to the relation later

For instance, it would be logical (and easy to remember) if

a relation between a Customers table and an Orders table were named CustomerOrders You will see more on

DataRelations later.

Makes relational navigation possible

Relational navigation is possible in ADO.NET simply because you can create relations by which to navigate! As

DataRelation objects are created using two DataColumn

objects, one from the parent and one from the child

DataTables, a connection by which to navigate exists.

Trang 26

RelationsCollection used to hold/group DataSet’s relations

As you have learned, DataRelations are retrieved or set using the DataSet object’s Relations property or the

DataTable object’s ParentRelations property Both

properties return a RelationsCollection, which is a collection of all DataRelation objects of a DataSet.

Trang 27

ADO.NET 27

Core Concepts and Architecture

Creating Relations With DataRelation

Keeping in mind the previous example of creating

DataTable and DataColumn objects, let us explore how

to create relations between the Customers DataTable and

a second Orders DataTable object.

Three basic steps are all that are needed:

1 Retrieve the DataColumn objects that you will base the relation on from the appropriate DataTable using the DataSet’s Tables property.

2 Create a named DataRelation using the columns.

3 Add the relation to the DataSet using the DataSet’s

Relations property.

Trang 28

Core Concepts and Architecture

XML and the DataSet

DataSet can read/write XML for its data and/or schema

As mentioned previously, the DataSet is capable of

reading and writing its data and schema as XML This is important if you consider that XML is an open, industry standard that is popular among most software solutions providers At this time, XML is pervasive, found both in Internet solutions and in traditional applications The fact that it is designed to both contain and describe data (data that describes itself) makes it the perfect choice for a

universal data container such as the DataSet!

Furthermore, the ability of a DataSet to both read and

write data and schema as XML makes it possible for you

to both create and modify data in a DataSet using XML or

XML enabled solution, such as Microsoft SQL Server 2000

XML-related DataSet Methods for Reading

ReadXml: Reads an XML schema and data into the DataSet

ReadXmlSchema: Reads an XML schema into the DataSet

And for writing:

The methods for reading XML into a DataSet of course

have complimentary means of writing XML from a

DataSet: WriteXml and WriteXmlSchema Two

additional methods provided are: GetXml and

GetXmlSchema These methods return the data or

schema as a string

Trang 29

ADO.NET 29

Namespace property: Sets the namespace for serialization

Full support for SQL Server-style DiffGrams

DiffGrams are an XML means to track changes to a

DataSet's data and schema They provide a “before and

after” picture of things, if you will

Trang 30

Core Concepts and Architecture

Methods of Reading and Writing XML

This code sample shows you how to use some of the

XML-specific methods and properties of a DataSet.

Ngày đăng: 18/04/2014, 10:16

Xem thêm

w