aspnet
aspnet
Accessing data with ADO.NET
Accessing data with ADO.NET
Hà Đồng Hưng
Objectives
•
Introduction
–
ADO.NET and other technologies
•
Connected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected Objects in VS.NET
•
Disconnected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected objects in VS.NET
Objectives
•
Introduction
–
ADO.NET and other technologies
•
Connected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected Objects in VS.NET
•
Disconnected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected objects in VS.NET
What is ADO.NET ?
•
ADO.NET is a set of libraries included with the Microsoft
.NET Framework that help you communicate with various
data stores from .NET applications
–
connecting to a data source
–
submitting queries
–
processing results
•
Can also use ADO.NET as a hierarchical, disconnected data
cache to work with data offline
Why a new data access technology?
•
Visual Basic 3 - Data Access Objects (DAO)
–
designed to communicate with local file-based databases
•
Visual Basic 4 - Remote Data Objects (RDO)
–
a fast, lightweight data access layer designed to talk to larger server-based
databases (SQL Server, Oracle…)
•
Visual Basic 5 and Visual Studio 97 - ODBCDirect
–
combine the power of RDO with the ease of use that DAO provides
•
Visual Basic 6 and Visual Studio 6 - ADO
–
a data access model we could use easily in server-side scripts (fewer lines
of code, allow us to pass data structures from server to client and back)
Drawbacks of older technologies
•
Problems:
–
Developers need to build more powerful applications
•
work with XML data
•
can pass ADO Recordset objects between different tiers in your
application, but cannot combine the contents of multiple
Recordset objects
•
…
–
Recent versions of ADO added more XML features, but ADO will
never handle XML data as efficiently as ADO.NET)
ADO.NET model
•
ADO.NET is designed to help developers build efficient multi-
tiered database applications across intranets and the Internet
ADO.NET model
•
The disconnected half of the ADO.NET object model do not
communicate directly with the connected objects
•
A major change from previous Microsoft data access object
models.
–
In ADO, Recordset object stores the results of your queries.
–
Call Open method to fetch the results of a query
–
Call Update method to submit changes stored within the Recordset to your
database.
•
ADO.NET DataSet is comparable in functionality to the ADO
Recordset.
–
However, DataSet does not communicate with your database.
–
In order to fetch data from your database into a DataSet, you pass the
DataSet into the Fill method of a connected ADO.NET object—the
DataAdapter.
.NET Data Providers
•
SQL Client .NET Data Provider
–
communicate with SQL Server databases, version 7+
•
OLE DB .NET Data Provider
–
communicate with various data stores through OLE DB providers
•
Each .NET data provider implements the same base classes
–
Base classes: Connection, Command, DataReader, Parameter,
Transaction
–
their actual names depend on the provider
Connection Strings
•
SQL Server
–
Provider=SQLOLEDB;Data Source=MyServer\MyInstance;
Initial Catalog=MyDatabase;User ID=MyUID;Password=MyPass;
–
Provider=SQLOLEDB;Data Source=MyServer;
Initial Catalog=MyDb; Integrated Security=SSPI;
–
Provider=SQLOLEDB;Data Source=MyServer;
Initial Catalog=MyDatabase; Trusted_Connection=Yes;
•
Oracle
–
Provider=MSDAORA;Data Source=MyDatabaseAlias;
User ID=MyUID;Password=MyPass;
•
Access
–
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\A\MyDb.mdb;
–
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data\MyDb.mdb;
–
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\A\MyDb.mdb;
Jet OLEDB:Database Password=MyPass;
Objectives
•
Introduction
–
ADO.NET and other technologies
•
Connected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected Objects in VS.NET
•
Disconnected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected objects in VS.NET
Using ADO.NET Objects
Connected Objects
•
Connection Object:
–
Use it to connect to and disconnect from your database.
•
Command Object
–
Represent a query against your database (retrieve, modify…), a call
to a stored procedure, a direct request to return the contents of a
specific table
•
DataReader Object (data read-only)
–
Is designed to help you retrieve and examine the rows returned by
your query quickly
•
Transaction Object
•
Parameter Object
Connected Objects
•
Data Adapter
–
act as a bridge between your database and the disconnected objects
in the ADO.NET object model
–
Fill method fetch the results of a query into a DataSet or a
DataTable
–
Update method submit the cached changes in your DataSet to your
database.
Objectives
•
Introduction
–
ADO.NET and other technologies
•
Connected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected Objects in VS.NET
•
Disconnected Objects
–
Objects List
–
Demonstrations
–
Using Disconnected objects in VS.NET
Connected Objects
Demonstrations
Dim strConn As String = "Provider=Microsoft.Jet.OleDB.4.0; & _
“Data Source=db1.mdb"
Dim cn As New OleDbConnection(strConn)
cn.Open()
...
cn.Close()
•
Open connection