Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to Connect to Database ODBC (Open Database Connectivity) ADO (ActiveX Data Object) ASP Code To Connect to Database Recordset Object Navigating through.

Similar presentations


Presentation on theme: "How to Connect to Database ODBC (Open Database Connectivity) ADO (ActiveX Data Object) ASP Code To Connect to Database Recordset Object Navigating through."— Presentation transcript:

1 How to Connect to Database ODBC (Open Database Connectivity) ADO (ActiveX Data Object) ASP Code To Connect to Database Recordset Object Navigating through Recordset

2 ODBC Stands for Open Database Connectivity Is a standard database access method developed by the SQL Access group in 1992 Has the goal of making it possible to access any data from any application, regardless of DBMS being used Uses a middle layer called a driver between the application (e.g., SQL program) and the DBMS (e.g., MS Access)

3 ODBC SQL Applications (Programs) ODBC Driver Manager (provided by MS) ODBC drivers for specific DB (provided by specific vendor) SQL DBMS or Access DBMS

4 ADO Stands for ActiveX Data Objects Is a high-level interface for accessing data objects—way of looking at data objects (developed by Microsoft) What is ADO’s relationship to ASP? o ASP code can manipulate ADO objects, using VBScript sintax Sample code o See later slides

5 ADO on the Web Server BrowserServer ASP Engine ADO Objects 1.Browser requests ASP page from Server 2.Server sends ASP page to ASP Engine 3.ASP Engine executes Server-side scripts 4.ASP Engine calls ADO objects (if any) 5.ADO objects do their thing 6.ASP Engine returns HTML to Server 7. Server returns HTML to Browser 1 7 3 2 6 4 5

6 ADO & COM ADO (ActiveX Data Objects) objects are COM (Component Object Model) objects specifically designed to interact with ODBC objects. The purpose of ADO objects is to gain access to database through ASP ODBC (Open Database Connectivity) -- standard database access method developed by MS --Access, dBase4, Paradox, FoxPro, etc.

7 ADO Objects Command o to give command to a database Connection o link between ASP application and the database RecordSet o will be used most often o container for a “Cursor”, a temporary table

8 To Access Database from ASP Page 1.Form an SQL statement. 2.Create and Open ADO connection to the database. 3.Create and Open ADO recordset object. 4.Extract data from the recordset 5.Close recordset object 6.Close connection object

9 To Form SQL Statement <% ' Form SQL String Dim strSQL strSQL="" strSQL=strSQL & " SELECT firstName, lastName " strSQL=strSQL & " FROM Customers “...... %>

10 To Connect to Database // Assume that strSQL contains SQL statement to be // executed. Dim conn, recSet Set conn=Server.CreateObject(“ADODB.Connection”) conn.Provider=”Microsoft.Jet.OLEDB.4.0” conn.Open Server.MapPath(“mydatabase.mdb”) Set recSet = Server.CreateObject("ADODB.Recordset") conn.Open strSQL conn

11 ASP Code to Connect to Database (Alternate) // Assume that strSQL contains SQL statement to be // executed. Dim DBSource, conn, recSet DBSource = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ Server.MapPath("dealer.mdb") Set conn = Server.CreateObject("ADODB.Connection") Set recSet = Server.CreateObject("ADODB.Recordset") conn.Open DBSource ' Execute SQL recSet.Open strSQL, conn, 1, 1

12 Connecting to DB (Alternate) 1.Create SQL Statement strSQL=“SELECT firstName, lastName FROM Customers” 2.Connect to Physicsl DB DBSource = "Provider=Microsoft.Jet.OLEDB.4.0; _ Data Source=" & Server.MapPath("dealer.mdb") oCreate Connection Object Set conn = Server.CreateObject("ADODB.Connection") Create RecordSet object Set recSet = CreateObject(“ADODB.RecordSet”) Open the RecordSet recSet.Open strSQL, conn, 1, 1

13 Complete Code for Connecting to Database <% Dim conn, recSet, strSQL ' Form SQL String strSQL="" strSQL=strSQL & " SELECT firstName, lastName " strSQL=strSQL & " FROM Customers " ' Open database connection Set conn=Server.CreateObject(“ADODB.Connection”) conn.Provider=”Microsoft.Jet.OLEDB.4.0” conn.Open Server.MapPath(“mydatabase.mdb”) Set recSet = Server.CreateObject("ADODB.Recordset") conn.Open strSQL conn %>

14 RecordSet Current Record Pointer

15 Navigating through a RecordSet If recSet.EOF and recSet.BOF Then ‘ no records Response.Write(“No matching records found”) Else Do While Not recSet.EOF val1 = recSet(“firstName”) val2 = recSet(“firstName”) Reponse.Write(val1 & “ “ val2 & “ ”) recSet.MoveNext Loop End If

16 Complete Code <% Dim strSQL strSQL="" strSQL=strSQL & " SELECT firstName, lastName " strSQL=strSQL & " FROM Customers " ' Open database connection Set conn=Server.CreateObject(“ADODB.Connection”) conn.Provider=”Microsoft.Jet.OLEDB.4.0” conn.Open Server.MapPath(“mydatabase.mdb”) Set recSet=Server.CreateObject(“ADODB.Recordset”) conn.Open strSQL, conn %>

17 All Customers All Customers <% Do While not recSet.eof Response.Write(recSet(“firstName") & " " & _ recSet("nameLast") & " " ) recSet.MoveNext loop recSet.close Set recSet = Nothing conn.close Set conn = Nothing %>

18 RecordSet Properties BOF o beginning of a recordset EOF o end of a recordset RecordCount o number of records

19 RecordSet Methods AddNew o Add new record to the record set Close o Close the recordset Delete o Delete the current record MoveNext o Move record pointer to the next record Open o Open the recordset (execute the query)


Download ppt "How to Connect to Database ODBC (Open Database Connectivity) ADO (ActiveX Data Object) ASP Code To Connect to Database Recordset Object Navigating through."

Similar presentations


Ads by Google