ado.net

61 97 0
ado.net

Đ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

ADO.NET 2 ADO.NET Overview 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.NETADO.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 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. 4 ADO.NET Overview ADO vs. ADO.NET (1/2) ADO You may wonder what the difference between ADO and ADO.NET is, besides the name change. The long and short of it is that ADO.NET is the next step for ADO, consisting of many enhancements. Let us begin by looking at ADO. 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. ADO.NET 5 However, a connected architecture is impractical (if not impossible!) in the disconnected world of the Internet. For an Internet-based solution, you should certainly look at ADO.NET as your data access technology. 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. 6 ADO.NET 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. 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! 8 ADO.NET 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.” ADO.NET 9 Overview Benefits of ADO.NET We have already touched on the benefits of ADO.NET verses ADO in our comparison. As you learned, XML is used throughout ADO.NET and this has its own benefits. 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. 10 ADO.NET Overview Visual Studio.NET Enhancements Typed programming—a programming style Typed programming is a programming style in which end- user words are used to construct statements or evaluate expressions. 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 DataSet.Customer(“Jones”).Balance to access the Customer table than by writing Table(“Customer”)(“Jones”).Column(“Balance”) The typed programming model is also easier to read and comprehend for both programmers and nonprogrammers! 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. [...]... with the information ADO.NET 13 Core Concepts and Architecture ADO.NET- related Namespaces You may think of ADO.NET as a pyramid in which the topmost, 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 14 ADO.NET Core Concepts and Architecture System.Data Namespace The ADO.NET object model... 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 ADO.NET- related Namespaces ADO.NET is organized into five main namespaces: • • • • • System.Data System.Data.OleDb System.Data.Common System.Data.SqlClient System.Data.SqlTypes The core namespaces of ADO.NET that you will work with are.. .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 12 ADO.NET Core Concepts and Architecture Core Concepts and Architecture The ADO.NET. .. 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 ADO.NET 19 Core Concepts and... and store data It is the means to work on and with your data! 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 • • • • • • • System.Data Contains the “main” classes of ADO.NET DataSet In-memory cache of data and data-related information... 16 ADO.NET 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. .. Concepts and Architecture System.Data Namespace The ADO.NET object model System.Data namespace is contained in the 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... myDataTableCollection; myDataTableCollection = myDataSet.Tables; // Iterate over the collection // Iteration not shown 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! 22 ADO.NET Core Concepts and Architecture The DataTable Maps DataSet data to a physical table in the data source You... 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 • Rows Returns the rows of the DataTable Technically, a RowsCollection of DataRow objects is returned ADO.NET • • • • 23 ParentRelations... combined objects comprise the table’s primary key 24 ADO.NET 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! ADO.NET 25 Core Concepts and Architecture Relating . more Recommended Reading • Microsoft ADO. NET newsgroup: microsoft.public.dotnet.framework.adonet ADO. NET 3 Overview ADO. NET and the .NET Framework ADO. NET is part of the Microsoft .NET Framework. If you break. security threat. 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. user interface), then ADO. NET sits squarely in the data and XML section. 4 ADO. NET Overview ADO vs. ADO. NET (1/2) ADO You may wonder what the difference between ADO and ADO. NET is, besides the

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

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan