Download presentation
Presentation is loading. Please wait.
Published byJuliet Merritt Modified over 8 years ago
1
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 Objectives (ASP)
2
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. Material
3
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
4
To retrieve information from a database, you need to use a two-step process 1. Establish a connection to the database 2. 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 database 2. Query the database, asking for information ADO provides two useful objects: the Connection object and the Recordset object. What is ADO? Using databases
5
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
6
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
7
ASP Page ActiveX Data Object (ADO) OELDB ODBCAccessSQLOther Data Providers
8
The ADO model contains six objects Connection object - connects to data source Recordset object - work with data in a table Error object - represents an error generated by data source Field object - represents a single column in the table Command object - provides another way to create a recordset object Parameters object - contains any parameters needed by the command The ADO model contains six objects Connection object - connects to data source Recordset object - work with data in a table Error object - represents an error generated by data source Field object - represents a single column in the table Command object - provides another way to create a recordset object Parameters object - contains any parameters needed by the command ActiveX Data Objects ADO
9
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 DSN 2. 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 DSN 2. 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
10
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 objConn Set 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.Close Set 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 objConn Set 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.Close Set objConn = Nothing This closes the connection to free the memory. Using a System DSN
11
This is an alternative to using a System DSN (e.g. to c:\my documents\WidgetWorld.mdb) … DSN-less Connection (P512) Dim objConn Set 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 to DBQ = 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 objConn Set 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 to DBQ = line tells where on the server the database resides. Using a DSN-less Connection
12
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
13
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 Recordset object. (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 Recordset object. (P516) Connection to a database
14
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 objRS Set objRS = Server.CreateObject ("ADODB.Recordset") The Open Method recordset.Open source, connection, cursortype, locktype, commandtype Source: Command Object, or string containing recognized command Connection: Connection Object, or string containing connection info cursortype: the way to move through recordset, default 0, forward, Day18 locktype: whether you can write to the table, default 1, readonly, Day 17 commandtype: 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 objRS Set objRS = Server.CreateObject ("ADODB.Recordset") The Open Method recordset.Open source, connection, cursortype, locktype, commandtype Source: Command Object, or string containing recognized command Connection: Connection Object, or string containing connection info cursortype: the way to move through recordset, default 0, forward, Day18 locktype: whether you can write to the table, default 1, readonly, Day 17 commandtype: how source should be evaluated, default 2, as table name The Recordset Object
15
Reading data from a database (P519) <% Const adCmdTable = 2 Dim objRS Set objRS = Server.CreateObject ("ADODB.Recordset") objRS.Open "tblExpenses", objConn,,, adCmdTable Do While Not objRS.EOF Response.Write "Name: " & objRS("EmployeeName") Response.Write " " objRS.MoveNext Loop objRS.Close set objRS=Nothing objConn.Close Set objConn=Nothing %>
16
Inserting, updating & deleting records (Day17) <% Const adLockOptimistic = 3 Const adCmdTable = 2 Dim objRS Set objRS = Server.CreateObject ("ADODB.Recordset") objRS.Open "tblUsers", objConn,, adLockOptimistic, adCmdTable objRs.AddNew objRs("FirstName") = "Gene" objRs("LastName") = "Williams" objRs("Email") = "blank@blah.net" objRs("Username") = "ww123" objRs("Password") = "pass" objRs.Update objRS.Close set objRS=Nothing objConn.Close Set objConn=Nothing %>
17
A Simple Project 3 Diagram Announcements
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.