Professional Documents
Culture Documents
ADO.NET
Agenda
Introduction to ADO.NET
Programming with ADO.NET
DAAB
Session Summary
Introduction to ADO.NET
What is ADO.NET?
Scalability
Targets distributed, disconnected web scenarios
High-performance design
Optimized providers and Objects
Introduction to ADO.NET
ADO ADO .NET
RecordSet
DataReader
DataAdapter
DataSet
Command
Command
Connection
Connection
Introduction to ADO.NET
ADO.NET and the .NET Framework
Web Services
User Interface
XML
...
...
Base Classes
Common Language Runtime
Introduction to ADO.NET
Managed Data Overview
Controls,
Designers,
Code-gen, etc
DataSet
DataAdapter
Sync
DataReader
Command
Connection
Managed Provider
Technology Solutions Lab
XSL/T, X-Path,
Validation, etc
XmlDataDocument
XmlReader
XmlText- XmlNodeReader
Reader
Introduction to ADO.NET
ADO.NET in action
Windows Forms
DataSet
Business Tier
Web Forms
Internet
Intranet
DataSet
XML
Data Tier
Data Adapter
Data Adapter
Business to Business
Introduction to ADO.NET
Namespace Organization
Introduction to ADO.NET
Data access stack
Agenda
Introduction to ADO.NET
Programming with ADO.NET
DAAB
Session Summary
10
data providers
SQL Server .NET Data Provider
Oracle .NET Data Provider
OLE DB .NET Data Provider
ODBC .NET Data Provider
Managed provider for retrieving XML from SQL Server
2000 (refer:http://msdn.microsoft.com/library/default.asp?
url=/nhp/default.asp?contentid=28001300)
11
CreateCommand
12
Example:
//Specify the System.Data.SQL Namespace
//using System.Data.SqlClient
//Create an instance of an SqlConnection object
SqlConnection cnn = new SqlConnection();
//Set the connection string
cnn.ConnectionString = "Data Source=.;Initial
Catalog=Northwind;Integrated Security=SSPI";
//Open the Connection
cnn.Open();
13
14
source
May be a SQL statement or stored procedure
Properties of Interest:
Connection: Get or set the data source connection
CommandText: Get or set the query (text)
command
15
Command.ExecuteReader
Ties up the Command until it is finished reading
Allows Strongly-Typed Access
Example: GetDateTime, GetDouble, GetGuid, GetInt32
Properties of Interest:
FieldCount: Returns the number of fields in the result set
RecordsAffected: Number of affected records
Is not set until all rows are read and you close the
DataReader
The number of rows changed, inserted, or deleted; 0 if no
rows were affected or the statement failed; and -1 for
SELECT statements.
IsClosed and RecordsAffected are the only properties that
you can call after the DataReader is closed.
16
17
18
SelectCommand
InsertCommand
UpdateCommand
DeleteCommand
DataSet
TableMappings
Technology Solutions Lab
19
MissingSchemaAction
20
DataSet
Tables
Table
Columns
Column
Constraints
Constraint
Rows
Row
21
DataSet
Tables
DataTable
DataView
DataRow(s)
Relations
DataRelation
DataRelation
DataColumn
Constraint(s)
DataViewManager
DataTable
DataTable
22
DataSet
Table1
Extracts tables
With Managed
Provider
Original
DataStore
Table2
Disconnected
Operations
Resolves changes
Sets up
relationships
with Managed
Provider
DataSet
DataSet
Table1
Table1
Makes changes
to data
Table2
Table2
23
data source
Can be related to one another through
DataRelations
Optimistic concurrency or locking - model
Properties of Interest:
Columns: Returns ColumnsCollection of DataColumns
Rows: Returns DataRow objects as a RowsCollection
ParentRelations: Returns the RelationsCollection
Constraints: Returns the tables
ConstraintsCollection
DataSet: Returns the DataSet of the DataTable
PrimaryKey: Gets the DataColumns that make up
the tables primary key
24
25
26
your data
Create relations between two (2) DataTable objects
Requires a DataColumn object from each DataTable
The DataType of both DataColumns must be the
same
Cannot relate a Int32 DataColumn and a String
DataColumn
The relation is named (by you!)
DataRelation dr=new DataRelation( myRelation,...)
them
Accessed through the DataSets Relations property
27
Example
// Building on the DataTable example earlier...
// Get the DataTable DataColumns we want to relate...
DataColumn parentCol, childCol;
parentCol=
DataSet.Tables["Customers"].Columns["CustID"];
childCol = DataSet.Tables["Orders].Columns["CustID"];
// Create DataRelation with the name CustomerOrders...
DataRelation dr = new DataRelation("CustomersOrders",
parentCol,
childCol);
// Add the relation to the DataSet...
ds.Relations.Add( dr );
28
WriteXml, WriteXmlSchema
GetXml, GetXmlSchema
Namespace property:
29
30
31
32
33
RegionDS.ReadXmlSchema( "Region_More.XSD" );
DataDoc.Load("Region_More.XML" );
//Now RegionDS contains data loaded from XML file
34
DataSet
Tables
DataTable
Relations
DataRelation
DataRelation
DataView
DataRow(s)
DataViewManag
er
DataColumn
DataViewSettings
Constraint(s)
DataViewSetting
DataViewSetting
DataTable
DataTable
35
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
36
37
CreateDataView method
Creates a DataView on a DataTable
38
39
1. adpter.Fill(ds,startrecord, maxrecords,sourceTableName)
2. switch (direction) //Next, Previous
{
case "Next":
selCmd.CommandText = "SELECT TOP " + pageSize + " CustomerID, CompanyName FROM
Customers " +
"WHERE CustomerID > @CustomerId ORDER BY CustomerID";
selCmd.Parameters.Add("@CustomerId", SqlDbType.VarChar, 5).Value = lastVisibleCustomer;
break;
case "Previous":
selCmd.CommandText = "SELECT TOP " + pageSize + " CustomerID, CompanyName FROM
Customers " +
"WHERE CustomerID < @CustomerId ORDER BY CustomerID DESC";
selCmd.Parameters.Add("@CustomerId", SqlDbType.VarChar, 5).Value = firstVisibleCustomer;
break;
default:
selCmd.CommandText = "SELECT TOP " + pageSize + " CustomerID, CompanyName FROM
Customers ORDER BY CustomerID";
// Determine total pages.
SqlCommand totCMD = new SqlCommand("SELECT Count(*) FROM Customers", nwindConn);
nwindConn.Open();
int totalRecords = (int)totCMD.ExecuteScalar();
nwindConn.Close();
totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
break;
}
40
Agenda
Introduction to ADO.NET
Programming with ADO.NET
DAAB
Session Summary
41
DAAB
42
DAAB
SqlHelper class
SqlHelperParameterCache class
URL: http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnbda/html/daab-rm.asp
43
Agenda
Introduction to ADO.NET
Programming with ADO.NET
DAAB
Session Summary
44
Session Summary
ASP.NET
IIS
Web
EAF
DataAccess
DAL1
DAAB
Base Components
BusinessRules
ADO.NET
Database
Technology Solutions Lab
45
Session Summary
Framework
Provides fast and efficient mechanisms for
connected and disconnected data access
46