Вы находитесь на странице: 1из 3

Database Connectivity Through ASP

=================================
ActiveX Data Objects(ADO) Library
ADODB
Classes in ADODB Library
=========================
ADODB.Connection
ADODB.Command
ADODB.RecordSet
ADODB.Parameter
ADODB.Field
Database terminology
======================
Database
Tables or Entity sets
Records or Tuples
Fields or Attributes or Columns
Database Connectivity through ASP
can be done in 2 ways:
1) Using DSN
2) Without using DSN
DSN: Data Source Name
=============================
To create a DSN
1) Goto Control Panel -> Administrative Tools
2) Select Data Sources (ODBC)
3) Goto User DSN or System DSN Tab
4) Click on ADD button
5) Choose Driver corresponding to your database
6) Enter the name for your DSN
7) If the database is already created, then click
on SELECT button to browse the database.
8) Finally click on OK to complete the DSN creation.
CONNECTION STRING
===================
1) For With-DSN Connections
Syntax:
"DSN=dsn_name"
Example:
"DSN=myDSN"
2) For DSN-Less Connection
Syntax:
ODBC
a) "DRIVER={driver name};DBQ=path\of\db"
OLEDB
b) "PROVIDER=provider.name;DATA SOURCE=path\of\db"
Examples:
a) "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\Users\Shri\db2.mdb"
b) "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\Users\Shri\db2.mdb"
SQL COMMANDS
==============
SELECT : To get the records from table
INSERT : To save new record into table
UPDATE : To edit existing table record
DELETE : To remove record from a table
Syntax:
1) SELECT *|colnames
FROM tablename
WHERE condition
ORDER BY colname ASC|DESC
2) INSERT INTO tablename (colnames)
VALUES (list of values)
3) UPDATE tablename
SET colname = newValue, ...
WHERE condition
4) DELETE FROM tablename
WHERE condition
Defining ADODB Objects in ASP
=============================
Syntax:
DIM varName
SET varName = Server.CreateObject("ADODB.classname")
Examples:
1)
DIM cn
SET cn = Server.CreateObject("ADODB.Connection")
2)
DIM cmd
SET cmd = Server.CreateObject("ADODB.Command")
3)
DIM rs
SET rs = Server.CreateObject("ADODB.RecordSet")
4)
DIM par
SET par = Server.CreateObject("ADODB.Parameter")
Using ADODB.Connection Object
===============================
Steps:
1) Define a connection object
Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
2) Define the ConnectionString property of this object
cn.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" +
Server.MapPath("db1.mdb")
3) Open the connection to the database
cn.Open
4) Execute the Queries on the databse tables as required
cn.Execute "SQL query"
5) Close the database Connection
cn.Close
Using ADODB.RecordSet Object
==============================
Steps:
1) Define the Recordset object
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
2) Define the ActiveConnection Property using a Connection
Object or Connection String
rs.ActiveConnection = "connection string"
e.g. rs.ActiveConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" &
Server.MapPath("db2.mdb")
3) Define the source command for fetching the records
rs.Source = "SELECT query"
4) Execute the associated Query using Open method
rs.Open
5) Browse the recordset until you reach EOF (i.e., rs.EOF),
fetching the individual field value of current record as:
rs.Fields(index).Value
or
rs.Fields("colname").Value
or
rs.Fields!colname
or
rs("colname")
4) Record Navigation can ebe done using the methods:
rs.MoveFirst
rs.MoveLast
rs.MoveNext
rs.MovePrevious
5) Finally close the recordset object
rs.Close
NOTE:
rs.EOF represents the End of File/Recordset
rs.BOF represents the Begining of File/Recordset
If Both (rs.BOF AND rs.EOF) are true at same time
then, there are no records fetched from database.

Вам также может понравиться