What is ADO

advertisement
What is ADO?





ADO
ADO
ADO
ADO
ADO
is a Microsoft technology
stands for ActiveX Data Objects
is a Microsoft Active-X component
is automatically installed with Microsoft IIS
is a programming interface to access data in a database
Accessing a Database from an ASP Page
The common way to access a database from inside an ASP page is to:
1.
2.
3.
4.
5.
6.
7.
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
8. Create a DSN-less Database Connection
9.
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 Access database on your web site.
10. If you have a database called "northwind.mdb" located in a web directory like "c:/webdata/", you
can connect to the database with the following ASP code:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
%>
11. Note, from the example above, that you have to specify the Microsoft Access database driver
(Provider) and the physical path to the database on your computer.
12.
13.
Create an ODBC Database Connection
14. If you have an ODBC database called "northwind" you can connect to the database with the
following ASP code:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Open "northwind"
%>
15. With an ODBC connection, you can connect to any database, on any computer in your network, as
long as an ODBC connection is available.
Connection Object
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.
If you want to access a database multiple times, you should establish a connection using the Connection
object. You can also make a connection to a database by passing a connection string via a Command or
Recordset object. However, this type of connection is only good for one specific, single query.
ProgID
set objConnection=Server.CreateObject("ADODB.connection")
Create an ADO Table Recordset
After an ADO Database Connection has been created, as demonstrated in the previous chapter, it is possible
to create an ADO Recordset.
Suppose we have a database named "Northwind", we can get access to the "Customers" table inside the
database with the following lines:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Customers", conn
%>
Open Database Connectivity (ODBC) is Microsoft's strategic interface for accessing data in a
heterogeneous environment of relational and non- relational database management systems. Based on
the Call Level Interface specification of the SQL Access Group, ODBC provides an open, vendor- neutral
way of accessing data stored in a variety of proprietary personal computer, minicomputer, and
mainframe databases.
OPEN DATABASE CONNECTION
The most basic purpose of ASP is to allow a website to connect to a database and show "Live
data". It is called live data because ideally the database administrator will be updating the
database routinely which will therefore automatically update the website.
So how do you do it? Well, it's actually pretty simple. First, you need to understand that there are
two ways to connect to a database. You can use a DSN or DSN-less connection, both accomplish
the same thing. A DSN is a Data Source Name that is setup on the server. You can think of it as a
shortcut to your database because it contains the driver and database path information to your
database. If you have your website hosted by an outside company like most people do, you will
need to contact them directly and ask them to setup the DSN for you. You will have to tell them
where your database is located within your website and you will have to give the DSN a name.
Not all hosting companies support ASP. If you are looking for a reliable hosting
company for your ASP website, we recommend using ASPWebHosting.com.
Here is an example of a DSN connection:
<%
DIM objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "DSN=myCONNECTION.dsn"
objConn.Open
%>
For those of you not familiar, we will run through the lingo. In the first section, the DIM line
declares the variable objConn. The "Set objConn..." sets the connection object.
"objConn.ConnectionString..." sets the connectionstring and the DSN and the last line
"objConn.Open" opens the connection.
Personnally, we prefer to use DSN-less connections to our databases. The reason is that for
maintenance and updating purposes, it is easier to make changes to database connections on your
own rather than having to call or email your hosting company and wait for them to update your
DSN.
There is a little more code involved with DSN-less connections, but it is worth it. Here is an
example of a DSN-less connection:
<%
DIM objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath ("/mydatabase.mdb") & ";"
objConn.Open
%>
The only difference between this example and the DSN example above is in the
"objConn.ConnectionString = ..." line. Instead of using "DSN = myCONNECTION.dsn" you
actually write out the appropriate driver and the respective path to the database. Ideally, the
connection string should all be written on one line, but for display purposes we put it on two.
When you paste this to your code, just remove the _ at the end of the line and put the
Server.MapPath on the same line.
For maintenance purposes, we recommend that you place your database connection in a
separate file like /includes/connection.asp. Then, simply use an include statement like
this:
<!--#INCLUDE VIRTUAL="/includes/connection.asp"-->
to include your connection string in your pages. Then, if your database connection
should ever change, you only have to edit it one time in your connection.asp file and it
updates your connection throughout your website.
Difference between oledb and odbc : oledb is the advanced version of odbc which does not require
dsn.
Download