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
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
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;
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
–
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