Download Data Access in .NET

Document related concepts
no text concepts found
Transcript
Data Access in
.NET Applications
Tarmo Tali
System Engineer
Microsoft Baltic
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
Session Prerequisites




Relational Database Design and
Programming
Visual Basic 6.0 / C++ Programming
Microsoft ADO
Understanding of XML
Important announcement!!!

Visual Studio.NET
is released!!!
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
ADO.NET
Background


Evolve ADO to multi-tier architecture
Interoperability


Scalability


Based on standards like XML, XSD
Targets distributed, disconnected web
scenarios
Productivity

Common programming model over
heterogeneous data
ADO.NET
Design Philosophy

Factored components

Explicit model; No black boxes!





Predictable behavior, semantics
Optimized performance
Disconnected Relational DataSet
Unified Data Story (data, xml, cache objects)



Customers/helper code wires together components
Data is Data; object model is choice
XML objects feel like XML, Data objects feels like
Data, Typed Objects feel like objects
Integrated into the Framework
ADO.NET
ADO.NET Architecture
Presentation Tier
Windows Forms
MyApp.exe
Data Set
Data Tier
Web Forms
IE
Data Set
Internet
Intranet
XML
Business Tier
Data Object (Class)
Data Set
SqlDataAdapter
SqlDataAdapter
Business to
Business
(BizTalk, for
example)
d
d
ADO.NET
Key Concepts


Native Support For XML
Focuses on Most Frequent

Streaming data



Client side static data



Large result sets
Maximum throughput and performance
In-memory relational view
Separation of data, view, and database implementation
specifics
Minimize Possibility For Mistakes


Server-side static cursors
Forget to MoveNext
ADO.NET
Versus ADO





Explicit Model for Updates
XML: built-in serialization and
relational mapping
Relational object model
ForEach rather than MoveNext
Extend schema at runtime
ADO.NET object model
Controls,
Designers,
Code-gen, etc
XSL/T, X-Path,
Validation, etc
DataSet
Sync
DataAdapter
DataReader
Command
Connection
Managed Provider
XmlDataDocument
XmlReader
XmlText- XmlNodeReader
Reader
ADO.NET
DataSet





DataSet
Tables
Table
In memory store for client,
Columns
middle-tier data
Column
Relational view of data
Constraints
XML to the core
Constraint
Rows
 Loads/saves data as XML
Row
 Loads/saves relational
Relations
schema as XSD
Relation
Disconnected, remotable object
No knowledge of data source
ADO.NET
Typed DataSet





Class generated by the designer
Inherits from DataSet
Schema coded into the class
Concise, readable code
Compile time type checking
ds.Tables(“Customers”).Rows(0).Columns(“Name”) = _
“Steve Ballmer”
ds.Customers(0).Name = “Steve Ballmer”
ADO.NET
DataAdapter




DataSetCommand
Manages exchange
between DataSet and
data source
ADO-like connections and
commands
Interacts with DataSet to
provide disconnected
functionality
Managed Provider SDK
Connection
SelectCommand
UpdateCommand
InsertCommand
DeleteCommand
TableMappings
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
Visual Studio .NET Tools




Component Designer
XML Designer
Schema Designer
Web Services/Web References
Visual Studio .NET Tools
Component Designer

DataAdapter Wizard
 Auto-generate
Update/Delete/Insert
statements
 Auto-config parameters collection for SQL
statements and stored procedures


Generate Typed Datasets
Table Mappings Dialog
 Map
database tables/columns to new or
existing Typed DataSets
Demo 1: Return Data from a
Web Service
SQL Server
IIS
Web Service
XML
Customers
DataSet
Command
DataSet
Access
DataSet
Command
Orders
Visual Studio .NET Tools
Web Services / Web References

Web Services
 Stream
DataSets over the wire as XML
 Publish schemas to client applications

Web References
 Discover
and consume methods, schemas
 Program against Web Services as objects
Demo 2: Windows Form vs
Web Service
Web Service
Windows Form
DataSet
XML
GetCustomers
DataSet
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
XML Designer
XML Documents and Schemas


XML Documents/Data
XSD Schemas
 W3C
recommended XML schema language
 Replaces DTDs
 Describes shape/structure of XML data
 Validation of XML data
 Contracts for data exchange
 Map to relational schemas
XML Designer
XML Documents and Schemas





Visually design XML Data and
XSD Schemas
Rich XML source editing
Drag/drop creation of schemas from
database
Infer schemas from XML data
Generate typed DataSets
Demo 3: XML Designer


XSD schema -> XML data
XML data -> XSD schema
Part I - Summary



Data = XML = Data
ADO.NET = distributed, disconnected,
scalable, XML-based data architecture
Visual Studio .NET = RAD for multi-tier
data apps
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
Introduction to ADO.NET
Comparison Between ADO and ADO.NET
Feature
ADO
ADO.NET
Memory-resident
Data Representation
RecordSet can contain
one table
DataSet can contain
one or more tables
represented by
DataTable Object
Relationship Between Require the JOIN query
Multiple Tables
Supports the
DataRelation object
Data Visitation
Uses a navigation
paradigm for nonsequential access
Scans RecordSet rows
sequentially
Disconnected Access Provided by RecordSet
but typically supports
connected access
Communicates with
standardized calls to
the DataAdapter
Introduction to ADO.NET
Comparison Between ADO and ADO.NET
Feature
ADO
ADO.NET
Programmability
Uses Connection object
to transmit commands
Uses strongly typed
programming
characteristics of XML
Sharing
Disconnected Data
Between Tiers or
Components
Uses COM marshalling
to transmit disconnected
record set
Transmits a DataSet
with an XML file
Transmitting Data
Through Firewalls
Problematic because
firewall are typically
configured to prevent
system-level requests
Supported, DataSet
object use XML, which
can pass through
firewalls
Scalability
Database locks and
active database
connections for long
Disconnected access
to database data
without retaining
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
ADO.NET object model
Controls,
Designers,
Code-gen, etc
XSL/T, X-Path,
Validation, etc
DataSet
Sync
DataAdapter
DataReader
Command
Connection
Managed Provider
XmlDataDocument
XmlReader
XmlText- XmlNodeReader
Reader
Programming with ADO.NET
Managed Providers

Manages Interaction To A Data Source





Managed equivalent of OLE DB layer
Directly exposes consumer interfaces
Not a full featured data store interface
Specific to (Optimized for) DataSource
Managed Provider Object Model



Connection
Command
DataReader
Programming with ADO.NET
Connection


Represents A Connection To The Data
Source
On A Connection, You Can…



Customize the connection to the database
Begin, commit, and abort transactions
Equivalent To The ADODB.Connection
Object
Programming with ADO.NET
Command Object

Represents A Command To Be Executed


With An ADO Command You Can:





Not necessarily SQL
Define a statement to be executed on the server
Set parameter information for that command
Retrieve return values from command execution
Corresponds To ADODB.Command Object
May Contain Parameters

Values to be used when executing a statement
Programming with ADO.NET
Command Object




ExecuteNonQuery
ExecuteReader
ExecuteScalar
ExecuteXmlReader (for SqlCommand
object only)
Programming with ADO.NET
DataSet


In Memory Store For Client Data
Relational View Of Data



Persist Data And Schema As XML
Explicit Disconnected Model



Tables, Columns, Rows, Constraints, Relations
Disconnected, remotable object
Array-like indexing
No Knowledge Of Data Source Or Properties


Common model over heterogeneous data
Predictable performance characteristics
Programming with ADO.NET
DataSet
DataSet
DataTable
DataColumn
DataTable
DataRow
Relations
XML Schema
Constraints
Programming with ADO.NET
Typed DataSet

Class Generated At Design Time



Inherits from DataSet
Schema coded into the class
Benefits



Statement completion
Compile time type checking
Concise, readable code
Programming with ADO.NET
DataAdapter

Manages Data Exchange Between
DataSet And Data Source




Provides Mappings Between Tables &
Columns
User Can Override Insert/Update/Delete
Commands


Fill(DataSet or DataTable)
Update(DataSet or DataTable)
Autogen component available
Allows Single DataSet To Be Populated From
Multiple Different Datasources
Programming with ADO.NET
DataAdapter
DataAdapter
Database
SelectCommand
InsertCommand
UpdateCommand
DeleteCommand
TableMappings
DataSet
Programming with ADO.NET
DataBinding

DataView





Thinks of this as a view on the DataTable
Allows setting Sort Order and Filter on a view
of the table
Any number of DataViews can be created on a
table to enable different views of the same table
Used for databinding
DataSetView




Think of this as a view on top of the DataSet
Allows setting sort orders and filters
Allows “linking” of DataViews
Used for databinding
Programming with ADO.NET
Sources for DataBinding








DataReader
DataTable
DataView
DataSet
DataSetView
Array
Collection
IList
Programming with ADO.NET
DataReader




Fast, Forward-Only, Read-Only Access
To Data
Works Like A Socket
Allows Strongly-Typed Access
Must be closed
Demo 4: Using ADO.NET

DataSet, DataAdapter and DataReader
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
XML Support
ADO.NET and XML

DataSet




Loads/saves XML data into/out of DataSet
Schema can be loaded/saved as XSD
Schema can be inferred from XML Data
XmlDataDocument




Exposes relational view over structured XML
Allows strong typing, control binding, relational
access of XML data
Allows XML tools (schema validation, XSL/T,
XPath queries) against relational data
Preserves full fidelity of XML Document
XML Support
XML View Of Relational Data

XmlDataDocument


DataDocumentNavigator




View/edit relational subset of XML document
within full XmlDocument
X/Path queries
XSL Transformations
Validation
SQL XML


Xml streamed from SQLServer
Support for UpdateGrams
XML Support
ADO.NET and XML

DataSet Provides Direct Handling Of
XML Documents and Schemas




ReadXml()
ReadXmlSchema()
WriteXml()
WriteXmlSchema()
Demo 5: XML Support

Read and Write XML Files
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
Advanced Features
ConnectionPooling


OLE DB Provider Utilizes Traditional
OLEDB Session Pooling
SQL Client Provider Utilizes COM+
Based Session Pooling
Advanced Features
ConnectionPooling
// Example in C#
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Integrated Security=SSPI; Initial
Catalog=Northwind”
conn.Open(); // Pool A is created;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Integrated Security=SSPI; Initial
Catalog=pubs”
conn.Open(); // Pool B is created because connection string
is different
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Integrated Security=SSPI; Initial
Catalog=Northwind”
conn.Open(); // Uses Pool A
Advanced Features
Distributed Transactions

To Use Distributed Transactions:




Use System.EnterpriseServices
Create a ServicedComponent to provide
automatic transaction support
Add features to the ServicedComponents
Excute transactions on the
ServicedComponents
Advanced Features
DataRelation

What Is DataRelation?



It is used to relate two DataTable objects to
each other
Relationships are created between
matching columns in the parent and child
tables
Relationships can also cascade various
changes to the parent row down to it’s
child rows
Advanced Features
Error Handling



ADO.NET Allows Developers To Add
Error Messages To Each Row Of Data In
A DataSet
You Can Filter For Rows In Error
Error Persist With The DataSet Even
When Being Transferred Using XML Or
Web Services
Demo 6
Error Handling
ADO and Error Handling
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
Demonstration 7
Converting ADO To
ADO.NET
Agenda

Part I




ADO.NET Overview
Visual Studio .NET Tools
XML Designer
Part II






Difference between ADO and ADO.NET
Programming with ADO.NET
XML Support
Advanced Features
Converting ADO to ADO.NET
When To Use What?
When To Use What?
Considerations



Connected Data Access
Disconnected Data Access
XML View of Relational Data
When To Use What?
Connected Data Access

Managed Providers

Connection, Transaction



Command, Parameters


Database Updates, Selects, DDL
DataReader


Connecting to DataSource
Starting/Ending Transactions
(FO/RO) Server Cursor
DataAdapter


Pushing data into Dataset
Reading changes out of DataSet
When To Use What?
Disconnected Data Access

DataSet

Remoting Results


Caching Results


Save Data as XML, Schema as XSD
User interaction


ASP.NET Cache
Persisting results


SOAP, WebMethods, Remoting
Scrolling, sorting, filtering
DataView, DataViewManager

Binding Windows controls
When To Use What?
XML Data Access

XML

XmlDocument



XPathNavigator


XmlDataDocument
Implements the W3C DOM level 1 and 2 core
Provides read-only random access
XslTransform

Supports XSLT 1.0 syntax
Session Summary




ADO.NET is a natural evolution of ADO
ADO.NET is designed to work with XML
ADO.NET is closely integrated with the
.NET Framework
ADO.NET provides fast and efficient
mechanisms for connected and
disconnected data access
Questions And Answers