Upload
bertram-hood
View
234
Download
0
Tags:
Embed Size (px)
Citation preview
ADO.Net TableAdapters ADO.Net TableAdapters and and
TableAdapterManagerTableAdapterManager
Code Camp 2008Code Camp 2008
Emmet GrayEmmet Grayhttp://home.hot.rr.com/grayehttp://home.hot.rr.com/graye
IntroductionIntroduction
Strongly-typed DataSetsStrongly-typed DataSets Based upon DataSet and DataTable classesBased upon DataSet and DataTable classes Wizard based (no need to hand code)Wizard based (no need to hand code) Graphical view of the dataGraphical view of the data
BenefitsBenefits Design-time type checkingDesign-time type checking IntellisenseIntellisense Slight performance increase*Slight performance increase*
Strongly-Typed DataSetStrongly-Typed DataSet
DisadvantagesDisadvantages Severe code bloat*Severe code bloat* Is often incorrectly used as a substitute for Is often incorrectly used as a substitute for
true n-tier “data access layer”true n-tier “data access layer” Use of wizard may cause you to overlook Use of wizard may cause you to overlook
other options such as stored proceduresother options such as stored procedures
* Performance increases may be overshadowed by code bloat* Performance increases may be overshadowed by code bloat
TableAdapterTableAdapter
Strongly-typed version of the DataAdapter Strongly-typed version of the DataAdapter classclass The typical way to get data in/out of a The typical way to get data in/out of a
strongly-typed DataSetstrongly-typed DataSet One “DataAdapter” per tableOne “DataAdapter” per table Has traditional Fill() and Update() methodsHas traditional Fill() and Update() methods Can be expanded with additional methodsCan be expanded with additional methods Concepts similar to that of stored proceduresConcepts similar to that of stored procedures All queries located in one placeAll queries located in one place
TableAdapter DisadvantagesTableAdapter Disadvantages
Tied to the current data provider (i.e. SQL Tied to the current data provider (i.e. SQL Server)Server)
To make a program database “agnostic” To make a program database “agnostic” you would:you would: Build the DataSet as usualBuild the DataSet as usual Remove all TableAdaptersRemove all TableAdapters Use the “Data Provider Factory”Use the “Data Provider Factory”
Schema BasedSchema Based
All queries in a TableAdapter must return All queries in a TableAdapter must return rows based upon the schema associated rows based upon the schema associated with the strongly-typed DataTablewith the strongly-typed DataTable Joins are allows for filtering (but only columns Joins are allows for filtering (but only columns
in the base table are allowed)in the base table are allowed) Exception for scalar queries (that return a Exception for scalar queries (that return a
single value)single value) Supports parameterized queriesSupports parameterized queries
QueriesQueries
So how do I handle custom queries that So how do I handle custom queries that return a different schema?return a different schema? Create a new TableAdapter!Create a new TableAdapter! Also creates a new strongly-typed DataTableAlso creates a new strongly-typed DataTable
Update() methodsUpdate() methods Automatically creates the “CommandBuilder” Automatically creates the “CommandBuilder”
equivalent Insert, Update, Delete commandsequivalent Insert, Update, Delete commands
Database IntegrityDatabase Integrity
Constraints upon the dataConstraints upon the data Unique / Primary keyUnique / Primary key Foreign KeyForeign Key
RelationshipsRelationships Strongly-typed DataSets support relationshipsStrongly-typed DataSets support relationships Enforces relationships at run-timeEnforces relationships at run-time Detects problems prior to saving to back-end Detects problems prior to saving to back-end
databasedatabase
Saving the dataSaving the dataTableAdapter.Update() methodTableAdapter.Update() method Scans the rows for changes and sends the Scans the rows for changes and sends the
changes to the back-end databasechanges to the back-end database
How does the TableAdapter handle How does the TableAdapter handle updates for queries based upon joins?updates for queries based upon joins? Poorly…Poorly… You’re are forced to write the update logic by You’re are forced to write the update logic by
handhand
Database Integrity RevisitedDatabase Integrity Revisited
Since strongly-typed DataSets support Since strongly-typed DataSets support relationships, I’m good to go… right?relationships, I’m good to go… right? That’s true for your locally cached version of That’s true for your locally cached version of
the datathe data But you must make sure you update the But you must make sure you update the
tables in the back-end database in the correct tables in the back-end database in the correct order!order!
Particularly important in foreign-key Particularly important in foreign-key relationshiprelationship
TableAdapterManagerTableAdapterManager
TableAdapterManagerTableAdapterManager New to VS2008New to VS2008 Automatically scans the relationships and Automatically scans the relationships and
builds an UpdateAll() method that updates the builds an UpdateAll() method that updates the tables in the correct ordertables in the correct order
Not really based upon a “base class”Not really based upon a “base class” Not tied to newer .Net Framework v3.xNot tied to newer .Net Framework v3.x
End NotesEnd NotesTableAdapterTableAdapter Based upon the associated schema of the Based upon the associated schema of the
strongly-typed DataTablestrongly-typed DataTable Handy place to keep all your queriesHandy place to keep all your queries May not be suitable for all ocasionsMay not be suitable for all ocasions
TableAdapterManagerTableAdapterManager New component to solve the problem of New component to solve the problem of
updating the back-end database tables in the updating the back-end database tables in the correct ordercorrect order