Presentation is loading. Please wait.

Presentation is loading. Please wait.

January 27, 2001Database Tutorial1 Using a Database in ASP.

Similar presentations


Presentation on theme: "January 27, 2001Database Tutorial1 Using a Database in ASP."— Presentation transcript:

1 January 27, 2001Database Tutorial1 Using a Database in ASP

2 January 27, 2001Database Tutorial2 Working with Databases Many ways to skin a cat We'll show basics Retrieve records Use ActiveX Data Objects (ADO) Delete records Add records Modify records

3 January 27, 2001Database Tutorial3 What Is A Relational Database? StudentAddressSchool Abe19704 SE 281stDelta Barbara231 45th Ave S.Delta Collete12397 22nd. Ave N. Wauseon DeeDee18209 NE 172ndDelta Freddy411 Main St.Swanton Gertrude202 S. MadisonWauseon Hecliffe912 E. Wood St. Delta SchoolAddress Delta401 Elm St Swanton721 Adrian Ave Wauseon13211 SE 92nd Field One or more tables of related data Record

4 January 27, 2001Database Tutorial4 Your Old Friend - A Web-Based Application - Your Home default.asp Your Form mynewform.html mydatabase.asp Your ASP Your Database

5 January 27, 2001Database Tutorial5 General Logic Flow Open your database mydatabase.asp Your ASP Your Database Send data back to the user Add, revise, delete rows Get the rows you need

6 January 27, 2001Database Tutorial6 Connection Object ASP Database Objects Created by createobject mydatabase Open method opens database Execute method creates recordset Recordset Objects Collete 12397 22nd. Ave N. Wauseon Gertrude 202 S. Madison Wauseon Freddy 411 Main St Swanton Abe 19704 SE 281st Delta Barbara 231 45th Ave S. Delta DeeDee 18209 NE 172nd Delta Hecliffe 912 E. Wood St. Delta Created by: Connection's execute method Createobject method Open method creates subset of table Has add, update, and delete methods

7 January 27, 2001Database Tutorial7 Opening Your Database Create a connection object dim sConnString 'Connection string sConnString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" _ & Server.MapPath("\millieneorr\db\rsFeedback.mdb") oConnection.open(sConnString) Set the connection string Open the database rsFeedback dim oConnection 'Connection object Set oConnection = server.createobject("ADODB.Connection") oConnection

8 January 27, 2001Database Tutorial8 Open your database Get the rows you need Add, revise, delete rows Send data back to the user

9 January 27, 2001Database Tutorial9 Getting the Rows You Need: The SELECT Command Syntax: SELECT MyField1, MyField2, MyFieldx FROM MyTable _ ORDER BY MyField2, Myfield3 _ WHERE MyField1="SomeValue" AND Myfieldx > 3 You can use "wild cards" SELECT * FROM MyTable ORDER BY MyField2 Legend: Red = Required Syntax Green = Optional Syntax Brown = Values you provide

10 January 27, 2001Database Tutorial10 Getting the Rows You Need: The RecordSet Object For reading only: –Create and populate with Connection object's execute method dim oFeedback_rs 'RecordSet object set oFeedback_rs = oConnection.execute("Select Name from tblFeedback") For reading and updating: –Create with server objects's CreateObject method –Populate with RecordSet's open method

11 January 27, 2001Database Tutorial11 Showing the Data You Retrieved dim str Do until oFeedback_rs.EOF str = oFeedback_rs("Name") response.write(str & " ") oFeedback_rs.MoveNext loop

12 January 27, 2001Database Tutorial12 Finding Stuff in Your RecordSet "Cursor" points to current record Methods for moving cursor: –MoveFirstMoveLast –MoveNextMovePreviousMove BOF and EOF Boolean properties indicate beginning and end of RecordSet Use "do until" to loop through records Field value is my_recordset_name("myfieldname")

13 January 27, 2001Database Tutorial13 Revising, Deleting, Adding Records Special parameters sent to RecordSet open method Locks record while you update Keeps fields in sync with other user updates

14 January 27, 2001Database Tutorial14 Revising Records Open RecordSet Modify fields in record Invoke RecordSet update method

15 January 27, 2001Database Tutorial15 Deleting Records Open RecordSet Navigate to appropriate record Invoke RecordSet delete method

16 January 27, 2001Database Tutorial16 Adding Records Open RecordSet Invoke RecordSet's AddNew method Set field values Invoke RecordSet's update method

17 January 27, 2001Database Tutorial17 Alphabet Soup S tructured Q uery L anguage A ctiveX D ata O bjects O pen D ata B ase C onnectivity O bject L inking and E mbedding OLE DB U niversal D ata A ccess

18 January 27, 2001Database Tutorial18 mydatabase.asp (Part 1) My First Database ASP Program <% On Error Resume Next 'Continue if error encountered dim oConnection 'Connection object dim sConnString 'Connection string dim oFeedback_rs 'RecordSet object dim str 'String variable '***** Example 1: Selecting records from a table response.write("*** Example 1: Selecting records from a table *** ") 'Create connection object, define connection string, open database Set oConnection = server.createobject("ADODB.Connection") sConnString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" _ & Server.MapPath("\millieneorr\db\rsFeedback.mdb") oConnection.open(sConnString)

19 January 27, 2001Database Tutorial19 Mydatabase.asp (part 2) 'Create recordset of names from all records in the feedback table set oFeedback_rs = oConnection.execute("Select Name from tblFeedback") 'Display the names Do until oFeedback_rs.EOF str = oFeedback_rs("Name") response.write(str & " ") oFeedback_rs.MoveNext loop oConnection.close 'close database 'You're done!! %>


Download ppt "January 27, 2001Database Tutorial1 Using a Database in ASP."

Similar presentations


Ads by Google