Beruflich Dokumente
Kultur Dokumente
NET
Objectives
Introduce Microsoft® ADO.NET
Show the evolution of ADO to ADO.NET
Introduce the primary components of ADO.NET
Contents
Differences Between ADO and ADO.NET
Benefits of ADO.NET
ADO.NET Core Concepts and Architecture
The ADO.NET Object Model
The DataSet and Data Views
Managed Providers
ADO.NET and the .NET
Framework
Microsoft .NET Framework
Base Classes
ADO
Designed for connected access
Tied to the physical data model
The RecordSet is the central data container
RecordSet is one (1) table that contains all the data
Retrieving data from > 1 table or source requires a database JOIN
Data is “flattened”: lose relationships; navigation is sequential
Data types are bound to COM/COM+ data types
Data sharing via COM marshalling
Problems marshalling through firewalls (DCOM, binary)
ADO vs. ADO.NET 2/2
ADO.NET
Designed for disconnected access
Can model data logically!
The DataSet replaces the RecordSet
DataSet can contain multiple tables
Retrieving data from > 1 table or source does not require a JOIN
Relationships are preserved: navigation is relational
Data types are only bound to XML schema
No data type conversions required
XML, like HTML, is plaintext: “Firewall friendly”
Benefits of ADO.NET
Interoperability through use of XML (more later!)
Open standard for data that describes itself
Human readable and decipherable text
Used internally but accessible externally
Can use XML to read and write and move data
Wizard support
Generates queries for you
Graphical way to select data you want to work with
XML Designer (for creating DataSets)
Core Concepts and
Architecture
The ADO.NET Object Model
Objects of System.Data
.NET data providers
ADO.NET namespace hierarchy
Organizes the object model
Includes:
System.Data
System.Data.OleDb
System.Data.Common
System.Data.SqlClient
System.Data.SqlTypes
ADO.NET-related
Namespaces
ADO.NET
System.Data
Relations DataColumn
DataTable
Working Data - The
DataSet
An in-memory cache of data from a data source
Common way to represent and manipulate data
Universal data container
Not just for use with databases
Logical or physical representation of data
Designed to be disconnected from the data source
Connect, execute query, disconnect
Can use XML
To read and write data
To read and write XMLSchema
Properties & Methods of
Interest
Collections are used to add & remove tables & relations
Properties of Interest:
Tables: Returns the collection of DataTable objects
Relations: Returns the collection of DataRelations
Namespace: Gets or sets the namespace of the DataSet
Using Properties Samples:
myDataSet.Tables.Add( myTable );
myDataTableCollection = myDataSet.Tables
All About Data!
DataRelation DataViewSetting
DataTable
DataTable
Viewing Data - The
DataView
Create multiple views on DataTable objects
Bindable to user interface controls
Properties of Interest:
Table: Retrieves or sets the associated DataTable
Sort: Gets or sets the table’s sort columns and sort order
RowFilter: Gets or sets the expression used to filter rows
RowStateFilter: Gets or sets the row state filter
None, Unchanged, New, Deleted, ModifiedCurrent, and others
Creating a DataView by
Example
// Code for myTable “Customers” with “Name” column not shown
DataView view1 = new DataView( myTable );
DataView view2 = new DataView( myTable );
// Bind to UI element(s)...
DataGrid myGrid = new DataGrid();
myGrid.SetDataBinding( view1, “Customer”);
//...
Viewing More Data -
DataViewManager
Similar to a DataView but DataSet oriented
Used to create multiple views on a DataSet
Ability to automatically set filters on the tables
Properties of Interest:
DataViewSettings: Gets the DataView for on each DataTable
DataSet: Gets or sets the DataSet to be viewed
CreateDataView method
Creates a DataView on a DataTable
DataViewManager By
Example
// Create the DataViewManager & views...
DataViewManager dvMgr = new DataViewManager( myDS );
dvMgr.CreateDataView( ds.Tables[“Orders"] );
dvMgr.DataViewSettings[“Orders"].Sort = “CustID ASC";
dvMgr.CreateDataView( ds.Tables[“Customers"] );
dvMgr.DataViewSettings[“Customers"].Sort = “Name DESC";
// Bind to a UI elements/controls...
dataGrid1.DataSource = viewMgr;
dataGrid1.DataMember = "Table1";
dataGrid2.DataSource = viewMgr;
dataGrid2.DataMember = "Table2";
Properties of Interest:
DeleteCommand: The delete command expressed in SQL
InsertCommand: Gets or sets insert command
SelectCommand: Gets or sets select command
UpdateCommand: Gets or sets update command
TableMappings: Maps source table and a DataTable
OleDbCommands retrieved or set by command properties
Implements abstract methods of the DataAdapter class:
public abstract int Fill( DataSet dataSet );
public abstract int Update( DataSet dataSet );
OleDbCommand Class
Properties of Interest:
FieldCount: Returns the number of fields in the result set
RecordsAffected: Number of affected records
Methods to retrieve data:
By column type and/or index: GetValue; GetString; etc.
Read(): Advances reader to next record
NextResult(): Advanced to next result set in batch
GetValues(): Gets the current row
OleDbDataReader Sample
// Code for creating the OleDbConnection “adoConn” not shown
String myQuery = “SELECT * FROM Customers”;
adoConn.Open();
OleDbCommand myCmd = new OleDbCommand( myQuery,adoConn );
IIS
Web
SystemFramework
Common.Data
BusinessFacade
BusinessRules
DataAccess
ADO.NE
T Database
Common Components
Duwamish7.Common
Contains systems configuration options
Contains common data definitions (classes)
Namespace Duwamish.Common.Data
"Internal" data representation for Book, Category,
Customer, OrderData
Duwamish7.SystemFramework
Diagnostics utilities
Pre and post condition checking classes
Dynamic configuration
In short:
Everything that's pure tech and not business code
Duwamish7.DataAccess
Contains all database-related code
Uses ADO.NET architecture
Using SQL Server managed provider
Shows DataSet, DataSetCommand usage
Optimized for performance by using stored procs
Duwamish7.BusinessRule
s
Implements all business rules
Validation of business objects (for example,
Customer EMail)
Updating business objects
Calculations (Shipping Cost, Taxes)
All data access performed through DataAccess
Duwamish7.BusinessFaca
de
Implements logical business subsystems
CustomerSystem: Profile management
OrderSystem: Order management
ProductSystem: Catalog
Reads data through DataAccess
Data validated and updated using BusinessRules
BusinessFacade encapsulates all business-related
functionality
Duwamish7.Web
Implements the user interface for Web access
Uses ASP.NET architecture
Employs Web Forms model
Uses code behind forms
Manages state
Uses custom Web Controls
All functionality accessed through BusinessFacade
Shop at Duwamish
Online.NET
Demo: Duwamish in Action
Exploring Duwamish
ADO.NET
Exploring ADO.NET Features in Duwamish
Legal Notices
Unpublished work. 2001 Microsoft Corporation. All rights
reserved.
Microsoft, Visual Basic, Visual Studio, and Windows are either
registered trademarks or trademarks of Microsoft Corporation
in the United States and/or other countries.
The names of actual companies and products mentioned
herein may be the trademarks of their respective owners.