•ActiveX Data Objects (ADO) · Access Objects) , then RDO (Remote Data Objects), and then ADO...

16
•Using databases •ActiveX Data Objects (ADO) •Connecting to a database •Reading data from a database •Inserting, updating and deleting records •Using databases •ActiveX Data Objects (ADO) •Connecting to a database •Reading data from a database •Inserting, updating and deleting records

Transcript of •ActiveX Data Objects (ADO) · Access Objects) , then RDO (Remote Data Objects), and then ADO...

•Using databases•ActiveX Data Objects (ADO)•Connecting to a database•Reading data from a database•Inserting, updating and deleting records

•Using databases•ActiveX Data Objects (ADO)•Connecting to a database•Reading data from a database•Inserting, updating and deleting records

����������� ��

This week’s material is from Day 15, 16 and 17 of the textbook.

Read the book.You can skip those statements/sessions that are not mentioned in this lecture.

This week’s material is from Day 15, 16 and 17 of the textbook.

Read the book.You can skip those statements/sessions that are not mentioned in this lecture.

��������

To store information through the use of a Web site, we can use cookies, the Session object and text files.All these methods have their drawbacks.

Databases are specifically designed to store massive amounts of information efficiently. Many commercial databases are available, including Access, MS SQL-Server, Oracle and Informix etc.It is possible to read and modify the contents of a database through an ASP page.Relational database ?

To store information through the use of a Web site, we can use cookies, the Session object and text files.All these methods have their drawbacks.

Databases are specifically designed to store massive amounts of information efficiently. Many commercial databases are available, including Access, MS SQL-Server, Oracle and Informix etc.It is possible to read and modify the contents of a database through an ASP page.Relational database ?

Using databases

To retrieve information from a database, you need to use a two-step process

1. Establish a connection to the database2. Query the database, asking for information

ADO provides two useful objects: the Connection object and the Recordset object.What is ADO?

To retrieve information from a database, you need to use a two-step process

1. Establish a connection to the database2. Query the database, asking for information

ADO provides two useful objects: the Connection object and the Recordset object.What is ADO?

Using databases

At one time, connecting to a database was difficult.Database came in a variety of formats, and you have to know low level API (Application Programming Interface) for every database you use.

A universal ASP came – ODBC (Open DataBase Connectivity) was developed. Many databases were known as ODBC-compliant.

Microsoft’s solution for ODBC = DAO (Database Access Objects) , then RDO (Remote Data Objects), and then ADO (ActiveX Data Objects)

At one time, connecting to a database was difficult.Database came in a variety of formats, and you have to know low level API (Application Programming Interface) for every database you use.

A universal ASP came – ODBC (Open DataBase Connectivity) was developed. Many databases were known as ODBC-compliant.

Microsoft’s solution for ODBC = DAO (Database Access Objects) , then RDO (Remote Data Objects), and then ADO (ActiveX Data Objects)

ActiveX Data Objects ADO

DAO, RDO, and ADO all have shortcomings.Microsoft introduced OLEDB, a COM-based data access object.

COM – Component Object Model – a model for binary code developed by Microsoft

OLEDB sort of replaces ODBC. It includes an ODBC driver so it is compatible with all the ODBC data source.

DAO, RDO, and ADO all have shortcomings.Microsoft introduced OLEDB, a COM-based data access object.

COM – Component Object Model – a model for binary code developed by Microsoft

OLEDB sort of replaces ODBC. It includes an ODBC driver so it is compatible with all the ODBC data source.

ActiveX Data Objects ADO

ActiveX Data Objects ADO

ASP Page

ActiveX Data Object (ADO)

OELDB

ODBC Access SQL OtherDataProviders

The ADO model contains six objects

Connection object - connects to data sourceRecordset object - work with data in a tableError object - represents an error generated by data sourceField object - represents a single column in the tableCommand object - provides another way to create a recordset objectParameters object - contains any parameters needed by the command

The ADO model contains six objects

Connection object - connects to data sourceRecordset object - work with data in a tableError object - represents an error generated by data sourceField object - represents a single column in the tableCommand object - provides another way to create a recordset objectParameters object - contains any parameters needed by the command

ActiveX Data Objects ADO

Before you can do anything with the database, you need to connect to it.

There are 2 ways to connect to a Access database.1. Using a System DSN2. Using a DSN-less connection

A system DSN is a file that contains information about the database such as where it is and what kind of database it is.Creating a system DSN : (P511)Control Panel - ODBC - System DSN

Before you can do anything with the database, you need to connect to it.

There are 2 ways to connect to a Access database.1. Using a System DSN2. Using a DSN-less connection

A system DSN is a file that contains information about the database such as where it is and what kind of database it is.Creating a system DSN : (P511)Control Panel - ODBC - System DSN

Connecting to a Database

After creating a DSN (e.g. WidgetWorld.dsn to connect to c:\my documents\WidgetWorld.mdb) …You can use the connection object in ASP … (P513)

Dim objConnSet objConn = Server.CreateObject (“ADODB.Connection”)objConn.ConnectionString = “DSN=WidgetWorld.dsn”objConn.Open

This creates the Connection object, set the connection string, and opens the connection.objConn.CloseSet objConn = Nothing

This closes the connection to free the memory.

After creating a DSN (e.g. WidgetWorld.dsn to connect to c:\my documents\WidgetWorld.mdb) …You can use the connection object in ASP … (P513)

Dim objConnSet objConn = Server.CreateObject (“ADODB.Connection”)objConn.ConnectionString = “DSN=WidgetWorld.dsn”objConn.Open

This creates the Connection object, set the connection string, and opens the connection.objConn.CloseSet objConn = Nothing

This closes the connection to free the memory.

Using a System DSN

This is an alternative to using a System DSN (e.g. to c:\my documents\WidgetWorld.mdb) … DSN-less Connection (P512)

Dim objConnSet objConn = Server.CreateObject (“ADODB.Connection”)objConn.ConnectionString = “DRIVER={Microsoft Access Driver (*.mdb)};” &_

“DBQ=C:\my documents\WidgetWorld.mdb”objConn.Open

Driver = line tells the kind of database it is connecting toDBQ = line tells where on the server the database resides.

This is an alternative to using a System DSN (e.g. to c:\my documents\WidgetWorld.mdb) … DSN-less Connection (P512)

Dim objConnSet objConn = Server.CreateObject (“ADODB.Connection”)objConn.ConnectionString = “DRIVER={Microsoft Access Driver (*.mdb)};” &_

“DBQ=C:\my documents\WidgetWorld.mdb”objConn.Open

Driver = line tells the kind of database it is connecting toDBQ = line tells where on the server the database resides.

Using a DSN-less Connection

Sometimes we do not have physical access to the server. Sometimes we want the code to be able to run anywhere. (so we cannot use c:\my documents\WidgetWorld.mdb)We can use Server.MapPath to find the physical path.

Dim objConn

Set objConn = Server.CreateObject("ADODB.Connection")

objConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &_ server.mappath("WidgetWorld.mdb")

Server.MapPath will find the physical path, so the code can run anywhere on from any physical location.

Sometimes we do not have physical access to the server. Sometimes we want the code to be able to run anywhere. (so we cannot use c:\my documents\WidgetWorld.mdb)We can use Server.MapPath to find the physical path.

Dim objConn

Set objConn = Server.CreateObject("ADODB.Connection")

objConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &_ server.mappath("WidgetWorld.mdb")

Server.MapPath will find the physical path, so the code can run anywhere on from any physical location.

Using a DSN-less Connection

We went through 3 ways to connect to a database

1. System DSN (create it under ODBC)2. DSN-less Connection (physical path)3. DSN-less Connection (Server.MapPath, relative path)

Now you know how to connect to a database.To work with data in the database, you need Recordsetobject. (P516)

We went through 3 ways to connect to a database

1. System DSN (create it under ODBC)2. DSN-less Connection (physical path)3. DSN-less Connection (Server.MapPath, relative path)

Now you know how to connect to a database.To work with data in the database, you need Recordsetobject. (P516)

Connection to a database

The Recordset object may be used to contain a subset of the records in a table, or even all the records in the table.To instantiate the Recordset object (P516)

Dim objRSSet objRS = Server.CreateObject ("ADODB.Recordset")

The Open Methodrecordset.Open source, connection, cursortype, locktype, commandtype

Source: Command Object, or string containing recognized commandConnection: Connection Object, or string containing connection infocursortype: the way to move through recordset, default 0, forward, Day18locktype: whether you can write to the table, default 1, readonly, Day 17commandtype: how source should be evaluated, default 2, as table name

The Recordset object may be used to contain a subset of the records in a table, or even all the records in the table.To instantiate the Recordset object (P516)

Dim objRSSet objRS = Server.CreateObject ("ADODB.Recordset")

The Open Methodrecordset.Open source, connection, cursortype, locktype, commandtype

Source: Command Object, or string containing recognized commandConnection: Connection Object, or string containing connection infocursortype: the way to move through recordset, default 0, forward, Day18locktype: whether you can write to the table, default 1, readonly, Day 17commandtype: how source should be evaluated, default 2, as table name

The Recordset Object

Reading data from a database (P519)

<!--#include file="connect02.asp"--><%Const adCmdTable = 2

Dim objRSSet objRS = Server.CreateObject ("ADODB.Recordset")objRS.Open "tblExpenses", objConn, , , adCmdTable

Do While Not objRS.EOFResponse.Write "Name: " & objRS("EmployeeName")Response.Write "<P>"objRS.MoveNext

Loop

objRS.Closeset objRS=NothingobjConn.CloseSet objConn=Nothing%>

Inserting, updating & deleting records (Day17)

<!--#include file="connect02.asp"--><%Const adLockOptimistic = 3Const adCmdTable = 2Dim objRSSet objRS = Server.CreateObject ("ADODB.Recordset")objRS.Open "tblUsers", objConn, , adLockOptimistic, adCmdTable

objRs.AddNewobjRs("FirstName") = "Gene"objRs("LastName") = "Williams"objRs("Email") = "[email protected]"objRs("Username") = "ww123"objRs("Password") = "pass"objRs.Update

objRS.Closeset objRS=NothingobjConn.CloseSet objConn=Nothing%>