Академический Документы
Профессиональный Документы
Культура Документы
NET:
Programming ADO.NET
Datasets
Vijaya Lakshmi Byri
Support Engineer
Manisha Gupta
Support Engineer
(PSS) Microsoft Corporation
Agenda
Review of ADO.NET architecture in the
Microsoft® .NET Framework
Working with datasets
ADO.NET datasets
XML and datasets
Typed datasets
2
Microsoft .NET Framework
3
ADO.NET Architecture
4
ADO.NET Datasets
Architecture of dataset
Creating a dataset
Navigating the dataset hierarchy
Copying a dataset
Merging a dataset
5
Dataset Architecture
Dataset
DataTableCollection
DataTable
DataRowCollection
DataColumnCollection
ConstraintCollection
DataRelationsCollection
6
Creating a Dataset
Server Explorer
Using DataAdapter
Programmatically
Using XML
7
Server Explorer
8
Generate a Dataset
9
Using DataAdapter
Open connection
Create a DataAdapter
Fill the dataset
10
Sample Code
11
Create Dataset Programmatically
It is possible to fabricate a dataset without
loading in external data.
1. Define some DataColumn
objects.
DataColumn
DataColumn
12
Create Dataset Programmatically (2)
DataTable
DataColumn
DataColumn
13
Create Dataset Programmatically (3)
3. Add the DataTable object to a Dataset object.
Dataset
DataTable
DataColumn
DataColumn
14
DataTable
Dataset
DataColumn
DataTable
DataRow
DataColumn
DataTable
DataRow
15
Sample Code
Dim myCustomers As New Data.DataTable()
myCustomers.TableName = "Customers"
With myCustomers
.Columns.Add("CustomerID", Type.GetType("System.String"))
.Columns.Add("CompanyName", Type.GetType("System.String"))
.Columns.Add("ContactName", Type.GetType("System.String"))
End With
myCustomers.Rows.Add(myDr)
Dim myDS As New Data.dataset("CustomerDS")
myDS.Tables.Add(myCustomers)
16
Navigating Dataset
Dataset
Dataset
TablesCollection DataTable
TablesCollection DataTable
ColumnsCollection DataColumn
ColumnsCollection DataColumn
RowsCollection DataRow
DataRow
RowsCollection
ConstraintsCollection Constraint
RelationsCollection
RelationsCollection DataRelation
DataRelation
17
Adding a DataRelation
custDS.Relations.Add("CustOrders", _
custDS.Tables("Customers").Columns("CustID"),
custDS.Tables("Orders").Columns("CustID"))
18
Navigating a Relationship
20
Creating an Exact Copy of the Dataset
21
Creating a Copy of Modified Rows
Use GetChanges to return only modified rows
with a specified row state.
Added
Updated
Deleted
22
Creating a Copy of the Schema
Use the Clone method to copy only the
schema of the dataset.
Use the ImportRow method of the DataTable
to add the existing rows.
24
Merging Factors and Options
Primary keys
PreserveChanges
MissingSchemaAction
Constraints
25
Sample Code
Dim ds As New Dataset("myDataset")
Dim t As New DataTable("Items")
' Create variable for temporary Dataset.
Dim xSet As Dataset
ds.Tables.Add(t)
‘Add two columns
‘Add Rows
ds.AcceptChanges()
‘Change row values
‘ Add one row.
‘Insert code for error checking.
‘ If the table has changes or errors, create a subset Dataset.
xSet = ds.GetChanges(DataRowState.Modified Or
DataRowState.Added)
‘Insert code to reconcile errors.
xSet.Tables("Items").Columns.Add(New DataColumn("newColumn"))
27
Loading a Dataset with XML Data
28
Sample Code
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
Console.WriteLine(dr.Item(0))
Next
29
XmlReadMode
Auto
DiffGram
Fragment
IgnoreSchema
InferSchema
ReadSchema
30
Writing a Dataset as XML
31
Sample Code
ds.WriteXml("C:\Customers.xml",XmlWriteMode.IgnoreS
chema)
32
Loading a Dataset with XSD Schema
ReadXmlSchema
Loads the XSD schema
Does not load any data
InferXmlSchema
Infers the XSD schema from the XML data
Loads the XSD schema
Loads the XML data
33
Writing XML Schema
GetXmlSchema
Returns an XSD schema
Return type is String
Dim xsdDS As String = DS.GetXmlSchema()
WriteXmlSchema
Writes XSD schema to a file or stream
DS.WriteXmlSchema("Customers.xsd")
34
Synchronizing XmlDataDocument
and Dataset
35
Synchronizing XmlDataDocument
and Dataset (2)
Create a dataset
Create an XmlDataDocument using a dataset
Dim cnNwind As New SqlConnection("Data
Source=(localhost);Integrated security=SSPI;Initial
catalog=Northwind;")
Dim daOrders As New SqlDataAdapter("Select * from
Orders where 1=2", cnNwind)
36
Performing XPath Queries on a
Dataset
Create an XmlDataDocument based on a
dataset
Perform XPath queries on the
XmlDataDocument
Dim nodes As Xml.XmlNodeList
nodes = xmldoc.SelectNodes("//OrderID")
Dim node as Xml.XmlNode
For each node in nodes
Console.WriteLine(node.InnerText)
Next
37
Performing XSL Transformations
on Dataset
Create an XMLDataDocument based on a
dataset
Using XslTransform object to transform the
data in the dataset
Dim xmldoc As New Xml.XmlDataDocument(ds)
Dim xsldoc As New Xml.Xsl.XslTransform()
xsldoc.Load("Orders.xsl")
Dim xpathnav As Xml.XPath.XPathNavigator
xpathnav = xmldoc.CreateNavigator
xsldoc.Transform(xpathnav, Nothing) 38
Typed Datasets
39
Using MSDatasetGenerator
40
IntelliSense® for Typed Dataset
41
IntelliSense for Typed Dataset (2)
42
Using .NET Command-Line Tools
Create a dataset using an XSD schema
Creates a class file (depending upon the language
specified)
Example Class1.vb
Syntax:
xsd.exe /d /l:C# XSDSchemaFileName.xsd
/n:XSDSchema.Namespace
Using language compiler compile the library
from the generator code
Syntax:
csc.exe /t:library XSDSchemaFileName.cs
/r:System.dll /r:System.Data.dll
43
Class1.vb
Namespace
Classes
x(xsd schema name)
Inherits dataset class
xDataTable
Inherits DataTable Class
xRow
Inherits DataRow Class
xRowChangeEvent
Inherits EventArgs Class
TypedName
TypedPlural
TypedParent
TypedChildren
NullValue
46
Thank you for joining us for today’s Microsoft Support
WebCast.
http://support.microsoft.com/webcasts/