Presentation is loading. Please wait.

Presentation is loading. Please wait.

DT221/3 Internet Application Development Active Server Pages & Database Connection.

Similar presentations


Presentation on theme: "DT221/3 Internet Application Development Active Server Pages & Database Connection."— Presentation transcript:

1 DT221/3 Internet Application Development Active Server Pages & Database Connection

2 Outline of ASP Database Connection Active Server Pages can communicate with databases via ADO (ActiveX Data Objects). 1)ADO Object Model 2)Database Connection 3)Record Set 4)Query Example 5)Update & Insertion Example 6)An Application Example

3 ADO Object Model 1)ADO is a Microsoft technology 2)ADO stands for ActiveX Data Objects 3)ADO is a Microsoft Active-X component 4)ADO is automatically installed with Microsoft IIS 5)ADO is a programming interface to access data in a database 6)ADO can be accessed from within your Active Server Pages

4 Relationship among the Objects

5 The normal way to access a database from inside an ASP page is to: 1)Create an ADO connection to a database 2)Open the database connection 3)Create an ADO recordset 4)Open the recordset 5)Extract the data you need from the recordset 6)Close the recordset 7)Close the connection Accessing a Database from an ASP Page

6 The easiest way to connect to a database is to use a DSN-less connection. A DSN-less connection can be used against any Microsoft SQL database on your web site. If you have a database called "northwind " located in your SQL Server, you can connect to the database with the following ASP code: Database Connection

7 1)Create a file called “ADOConnection.asp” and type in the code below <% dim connobj dim connstr connstr = "Provider=SQLOLEDB;Data Source=MyServer; Database=Northwind;User Id=NorthUser;Password=abc;" set connobj = Server.CreateObject("ADODB.Connection") connobj.Open (connstr) connobj.Close set connobj = nothing %> 2)The ADO Connection object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database. Database Connection, cont

8 Create the file “ADORecordset.asp” and type in the code below <% dim connobj dim connstr dim recobj connstr = "Provider=SQLOLEDB;Data Source=MYSERVER;Database=Northwind;User Id=NorthUser;Password=abc;" set connobj = Server.CreateObject("ADODB.Connection") connobj.Open (connstr)'this part create and assign the recordset. set recobj = Server.CreateObject("ADODB.Recordset") 'open the table customer using connobj as the default connection. recobj.Open "Customers", connobj Response.Write("Table Customers opened.") connobj.Close recobj.close set recobj = nothing set connobj = nothing %> Create an ADO Table Recordset

9 Create a file called “ADORecordset2.asp” and type in the code below: <% dim connobj dim connstr dim recobj connstr = "Provider=SQLOLEDB;Data Source=MYSERVER;Database=Northwind;User Id=NorthUser;Password=abc;" set connobj = Server.CreateObject("ADODB.Connection") connobj.Open (connstr) 'this part create and assign the recordset. set recobj = Server.CreateObject("ADODB.Recordset") 'open the table customer using connobj as the default connection. recobj.Open "Customers", connobj Response.Write("Table Customers opened. ") 'retrieve data Extract Data from the Recordset

10 for each x in recobj.fields response.write(x.name) response.write(" = ") response.write(x.value & " ") next connobj.Close recobj.Close set recobj = nothing set connobj = nothing %> Extract Data from the Recordset, cont

11 1)After using the recordset and connection object, it is important to properly close and destroy the objects. 2)Below is the 4 lines necessary to perform such operations. <% Recobj.close Connobj.close Set recobj = nothing Set connobj = nothing %> Closing Recordset and closing Connection

12 1)ODBC source connection example: Set myDBConnection = Server.CreateObject("ADODB.Connection" ) ConnStr = “DSN=airline db; uid=cis8490; pwd=test” myDBConnection.open connStr 2)OLE connection example Set myDBConnection = Server.CreateObject( "ADODB.Connection " ) myDBConnection.Open “PROVAIDER=SQLOLEDB; DATA SOURCE = yourserver; UID=sa; PWD=secret; DATABASE=Pubs ” 3)ODBC Driver For Trusted Connection security connection example Set myDBConnection = Server.CreateObject("ADODB.Connection " ) myDBConnection.Open "Driver={SQL Server};" & "Server=MyServerName;" & "Database=myDatabaseName;" & "Uid=;" & "Pwd=;" Database Connection Examples


Download ppt "DT221/3 Internet Application Development Active Server Pages & Database Connection."

Similar presentations


Ads by Google