Sei sulla pagina 1di 35

Dynamic database query generator for the administrator. Ask user the table, fields he wants to access.

Payal R. Chaudhary U07CO237 Purvi R. Chaudhary U07CO241 SVNIT,Computer department


1

Topics covered
PLATFORM USED WHAT IS ADO? What is ODBC? ADO, ODBC and OLE DB What is ADODB? Accessing a Database from an ASP Page

PLATFORM
Operating system- Windows Xp Back End Microsoft Office Access 2003 Database Front End ASP Access stores data in its own format based on the Access Jet Database Engine Jet is the Database Management System ( DBMS ) which underlies Microsoft Access Data is stored in the form of .mdb files
(database engine or "storage engine" is the underlying software component that a (DBMS) uses to create, read, update and delete data from a database)

ActiveX Data Objects (ADO)


ADO is Microsoft Technology ADO stands for ActiveX Data Objects ADO is a Microsoft ActiveX Component ADO is automatically installed with Microsoft IIS ADO is a programming interface to access data in a database ( without knowing how the Database is implemented )

ADO Software Architecture


VC++ VB Script Java

ADO

OLE DB

RDBMS

E-mail

Directory Services
5

What is ODBC?
ODBC (Open Database Connectivity) is an API specification that provides a way for client programs (eg Visual Basic, Excel, Access, etc) to access multiple database systems using SQL ODBC-standard software interface for accessing DBMS that uses SQL The ODBC Driver resides between the ODBC Client and the DBMS Any ODBC-compliant application can access any DBMS that has a corresponding driver.

ADO, ODBC and OLE DB


ODBC was the MS way of connecting to and using databases on the Windows platform ADO is the strategic way for using ODBC data sources on the Windows platform OLE DB extends ODBC by using the concept of service providers to add the ability to connect to other non-database data sources (MS Active Directory (LDAP), e-mail servers (Exchange)) that do not necessarily use SQL. ODBC Data Sources; provider = MSDASQL MS Index Server; provider = MSIDXS MS Active Directory Server - provider = ADSD300Object MS Jet databases - provider = Microsoft.Jet.OLEDB.3.51 MS SQL Server - provider = SQLOLEDB Oracle Databases - provider = MSDAORA
7

Confused !!!!....?
Lets go into little more detail

Providers and Drivers


Although ODBC drivers available for most popular DBMS,OLE DB is there for limited set of popular DBMS.To provide programmers access to both DBMS , Microsoft provided OLEDB provider for ODBC drivers Called MSDASQL
ADO

JET

SQL

Oracle

ODBC

OLE DB Providers

JET

SQL

Oracle

ODBC Drivers

Access

SQL

Oracle

Access

SQL

Oracle

Cont.
What the diagram explains?
OLE DB programming interface interacts with OLEDB data providers similarly to the way the ODBC API interacts with ODBC drivers-both OLEDB data provider and ODBC drivers provide direct access to specific data stores. Although ODBC drivers available for most popular DBMS, OLE DB is there for limited set of popular DBMS. To provide programmers access to both DBMS ,MS provided OLEDB provider for ODBC drivers.

10

What is ADODB?
ADODB(ActiveX Data Objects for Database) is an abstraction library adodb is a collection of methods to access the data from the Database Application developed using Microsoft Visual Studio ADODB supports-ADO,MYSQL,Oracle,MS SQL Sever,etc - it uses SQL

ADODB Objects

Connection

RecordSet

Command
11

Accessing a Database from an ASP Page(Steps)


Create an ADO connection to a database Open the database connection Create an ADO recordset Open the recordset Extract the data you need from the recordset Close the recordset Close the connection

Steps explained in detail for clear understanding.

12

STEP 1 :Creating instance of Connection


ADODB Connection object - open up an ODBC/OLEDB Connection to a database through database drivers so you can do something with the database.
Server Object- ASP object used to access properties and methods on the server

13

Creating a Connection
Connection objects are created using the CreateObject() function. <%

Dim ObjConn create a variable to hold a reference to the object Set ObjConn=Server.CreateObject(ADODB.Connection)

%>

14

Connection object required

15

STEP 2 : Open up a database


How???!!!!!! By using using 1) a Connection String (DSN-less) or 2) Data Source Name ( DSN ) Usually used is Connection String Establish a connection string and Open it Here we use DSN
16

1) If ConnectionString is used
Once the ConnectionObject created ,we need to give it a ConnectionString to logon to the datasource ConnectionString specifies the : provider datasource name userid (optional) password (optional) Dim cnn Dim str str = Provider=MSDASQL; Data Source=mydatasource; User Id=;Password= cnn.ConnectionString = str
17

Open() and Close()


Once the connection established , the Connection must be opened before using it Likewise it should be closed when done.
Dim cnn Dim str Set cnn = CreateObject(ADODB.Connection)

str = Provider=MSDASQL.1,Data Source=mydatasource; str = str & User ID=myuserid;Password=mypass cnn.ConnectionString = str

cnn.Open
..cnn.Close
18

2) If DSN is used . . .
<% Set MyConn=Server.CreateObject("ADODB.Connection") MyConn.Open "FILEDSN=c:\dsn\MyTable_dsn.dsn ... MYConn.close %>

19

DSN-Data Source Name


data structure that contains information about a specific database that an
ODBC driver needs in order to connect to it.(ex:- driver,directory,name as the database,user ID) A file DSN contains the relevant information within a text file with a .dsn file extension , and can be shared by users of different computers who have the same drivers installed.(unlike system and user DSN)

20

STEP 3 : CREATE AN ADO RECORDSET


Record set objects are the recipients of the result of a SQL query create the Connection object then create a recordset object and associate them

Dim MyConn Dim RS

set MyConn = CreateObject(ADODB.Connection)


set RS = CreateObject(ADODB.Recordset) assocoate the record set with the connection RS.ActiveConnection = MyConn
21

STEP 4 : Open the Recordset


SQL_query = "SELECT * FROM " & tablename
Set RS = MyConn.Execute(SQL_query)

RS.ActiveConnection = MyConn
RS.Open SQL_query

or

RS.Open SQL_query, MyConn

OR Set RS = MyConn.Execute(SQL_query)

Recordset creation and opening,in 1 Step

22

Step 5 : Extract the data you need from the recordset


The record set is a collection of rows, each row containing the data in the order that it was asked for in the SQL statement, each column in the row can be accessed by its column name User chooses table by simple radio botton, which gets passed to next page stored in a variable

23

Getting Data out


Displaying field names of table in checkboxes for user to Choose <tr><%for each x in rs.Fields%>
<th> <input type="checkbox" name="column value="<%Response.Write(x.name)%>"> <%Response.Write(x.name)%> </input> </th> <%next%></tr>
( for page 2 )
24

Displaying table with field names and values


<% SQL_query = "SELECT " & column & " FROM " & tablename Set RS = MyConn.Execute(SQL_query) %>

<table border="1" width="100%"><tr> <%for each x in RS.Fields Response.Write("<th>" & x.name & "</th>") next%></tr> <%do until RS.EOF%> <tr> <%for each x in RS.Fields%><td><%Response.Write(x.value)%></td> <%next RS.MoveNext%> </tr> <%loop ...
(

for page 3 )

25

Code and Output (Screenshots)

26

Page 1

Page 2

Page 3

Page 1
<HTML> <BODY> <form action="page2.asp" method="get"> <p>Please Enter the Table to display:</p> <input type="radio" name="tablename" value="Friends">Friends</input> <br /> <input type="radio" name="tablename" value="Saab">Subjects</input> <br /> <input type="radio" name="tablename" value="BMW">Family</input> <br /><br /><br /><br /> <input type="submit" value="Submit" /> </form> </BODY> </HTML>
30

Page 2
<HTML> <% dim tablename tablename=Request.QueryString("tablename") %> <BODY> <p>Please Enter the fields to Display :</p><br/><br/><br/> <form action="page3.asp" method="get"> <% Set MyConn = Server.CreateObject("ADODB.Connection") MyConn.Open "FILEDSN=c:\dsn\MyTable_dsn.dsn" SQL_query = "SELECT * FROM " & tablename Set rs = MyConn.Execute(SQL_query) %>

31

Page 2
You choosed : <input type="radio" name="tablename" value="<%Response.Write(tablename)%>" CHECKED><%Response.Write(tablename)%></input> <br/><br/><tr> <%for each x in rs.Fields %><th> <input type="checkbox" name="column" value="<%Response.Write(x.name)%>"><%Response.Write(x.name)%></input > </th><%next%> </tr></table> <% rs.close Set rs = Nothing MYConn.close Set MyConn = Nothing %> <br /><br /> <input type="submit" value="Submit" /></form> </BODY> </HTML>
32

Page 3
<HTML><% dim tablename dim column tablename=Request.QueryString("tablename") column=Request.QueryString("column")%> <BODY> <B>Here are my friends:<B><BR> <% Set MyConn = Server.CreateObject("ADODB.Connection") MyConn.Open "FILEDSN=c:\dsn\MyTable_dsn.dsn" SQL_query = "SELECT " & column & " FROM " & tablename Set RS = MyConn.Execute(SQL_query)%> <table border="1" width="100%"><tr> <%for each x in RS.Fields Response.Write("<th>" & x.name & "</th>") next%></tr>
33

Page 3 . . .
<%do until RS.EOF%> <tr> <%for each x in RS.Fields%> <td><%Response.Write(x.value)%></td> <%next RS.MoveNext%></tr><%loop RS.close Set RS = Nothing MYConn.close Set MyConn = Nothing %> <% if tablename<>"" then Response.Write("<p>Entered table is: " & tablename & "</p>") Response.Write("<p>Entered coloumn is: " & column & "</p>") end if %></BODY></HTML>

34

!!Thank you!!

35

Potrebbero piacerti anche