Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 375—Web App Dev II ADO I. 2 Introduction ADO (________ Data Objects) is a Microsoft technology for accessing data in a database. ADO is automatically.

Similar presentations


Presentation on theme: "CIS 375—Web App Dev II ADO I. 2 Introduction ADO (________ Data Objects) is a Microsoft technology for accessing data in a database. ADO is automatically."— Presentation transcript:

1 CIS 375—Web App Dev II ADO I

2 2 Introduction ADO (________ Data Objects) is a Microsoft technology for accessing data in a database. ADO is automatically installed with _____. The common way to access a database from within an ASP page is to: Create an ADO ___________ to a database Open the database connection Create an ADO __________ Open the recordset Extract the data you need from the recordset Close the recordset Close the connection

3 3 Database Connections Before a DB can be accessed from a web page, a DB connection must be established. The easiest way to connect to a DB is to use a DSN- _____ connection.DSN A DSN-less connection can be used against any _______________ DB on your web site.

4 4 Create a DSN for an ODBC DB Note that this configuration has to be done on the computer where your web site is located. 1. Open the ODBC icon in your Control Panel. 2. Choose the System DSN tab. 3. Click on Add in the System DSN tab. 4. Select the Microsoft Access Driver. Click Finish. 5. In the next screen, click Select to locate the database. 6. Give the database a Data Source Name (DSN). 7. Click OK. If your web site is located on a remote server, you must have _________ access to that server, or ask your web host to do this for you.

5 5 Create/Open a DB Connection For an Access DB called “hr.mdb" in a folder called “fpdb” with your ASP file in the _____ directory, this code creates and opens a DSN-less ADO connection. <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open(Server.Mappath(“fpdb/hr.mdb")) …%> If you have an ODBC DB with a _____ called “hr," connect to the DB with the following ASP code:ODBC <% set conn=Server.CreateObject("ADODB.Connection") conn.Open “hr" …%>

6 6 Create/Open a Recordset To be able to read database data, the data must first be loaded into a _________. If you opened the DB connection with the first set of code in the previous slide, you can access the “employee" table by adding the following code: set rs=Server.CreateObject("ADODB.recordset") rs.Open "employee", conn Or you could use _____ as follows: set rs=Server.CreateObject("ADODB.recordset") rs.Open "Select * from employee", conn This stores all table records in the recordset.

7 7 Display Data To extract data from a recordset and then close the recordset and connection, add the following code: do until rs.EOF for each x in rs.Fields ‘ “x” refers to a field Response.Write(x.name) ‘ field name Response.Write(" = ") Response.Write(x.value & " ") ‘ field value next Response.Write(" ") rs.MoveNext ‘ move to next record in table loop rs.close conn.close

8 Display Data in an HTML Table File named display_employees.aspdisplay_employees.asp <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open(Server.Mappath("fpdb/hr.mdb")) set rs=Server.CreateObject("ADODB.recordset") sql="SELECT * FROM employee" rs.Open sql, conn %> <% for each x in rs.Fields response.write(" " & x.name & " ") next %> <% for each x in rs.Fields %> <% Response.Write(x.value) %> <% next rs.MoveNext %> <% loop rs.close conn.close %>

9 9 Add Records I First create a form for data input (form.htm):form.htm SSN: Last Name: First Name:

10 10 Add Records II Then create the ASP file to add records (add.asp): <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open(Server.Mappath(“fpdb/hr.mdb")) sql="INSERT INTO employee (ssn,lastname,firstname) VALUES " sql=sql & "('" & Request.Form("ssn") & "'," sql=sql & "'" & Request.Form(“lastname") & "'," sql=sql & "'" & Request.Form(“firstname") & "')" on error resume next conn.Execute sql,recaffected if err<>0 then Response.Write("No update permissions!") else Response.Write(" " & recaffected & " record added ") end if conn.close %>

11 Update Records IUpdate Records I: hr_listforupdate.asphr_listforupdate.asp This file displays all records in a special way (continues on next slide): <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open(Server.Mappath("../db/hr.mdb")) set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM employee",conn %> List Database <% for each x in rs.Fields response.write(" " & ucase(x.name) & " ") next %>

12 hr_listforupdate.asp (continued) <% for each x in rs.Fields if x.name="ssn" then%> "> <%end if next %> <% loop conn.close %>

13 Update Records II: hr_update.asp Update Record <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open(Server.Mappath("fpdb/hr.mdb")) social=Request.Form("ssn") if Request.form("lastname")="" OR Request.form("firstname")="" then set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM employee WHERE ssn ='" & social & "'", conn %> " value=" " size="20">

14 hr_update.asp (continued) <% else sql="UPDATE employee SET " sql=sql & "lastname='" & Request.Form("lastname") & "'," sql=sql & "firstname='" & Request.Form("firstname") & "'" sql=sql & " WHERE ssn='" & social & "'" on error resume next conn.Execute sql if err<>0 then response.write("No update permissions!") else response.write("Record " & social & " was updated!") end if conn.close %>

15 15 Delete Records IDelete Records I: hr_listfordelete.asp hr_listfordelete.asp This file is identical to hr_listforupdate.asp except for the following code: instead of:

16 16 Delete Records II: hr_delete.asp This file is identical to hr_update.asp except for the following code: sql="DELETE FROM employee" sql=sql & " WHERE ssn='" & social & "'" instead of: sql="UPDATE employee SET " sql=sql & "lastname='" & _ Request.Form("lastname") & "'," sql=sql & "firstname='" & _ Request.Form("firstname") & "'" sql=sql & " WHERE ssn='" & _ social & "'"

17 17 ADO Demonstration How to change the ________ rights of your Access database: Open Windows Explorer, find the.mdb file. Right-click on the.mdb file and select ___________. Go to the _________ tab and set the access-rights here. List, edit, update, and delete database records Add a new record

18 18 Using Access & FrontPage SMSU Computer Services has a web page describing how to create a database connection using MS Access and FrontPage software.web page SMSU Computer Services also has a tutorial on how to set up a form on a web page and connect it to an Access database.tutorial


Download ppt "CIS 375—Web App Dev II ADO I. 2 Introduction ADO (________ Data Objects) is a Microsoft technology for accessing data in a database. ADO is automatically."

Similar presentations


Ads by Google