Você está na página 1de 4

INTRODUCTION TO DATABASE CONNECTIVITY

In computing, Open Database Connectivity (ODBC) provides a standard software API method for
using database management systems(DBMS) !he designers of "DB# aimed to ma$e it independent
of programming %anguages, database systems, and operating systems
Creating the Guestbook Database
To create a database your first need to open Microsoft Access and choose 'Blank Access Database'
from the starting menu. You will be prompted for a name for the database and where you want it
saved. all the database 'guestbook.mdb' and save it in the same directory as the web page
connecting to the database is going to be.
You should now see the main Access dialog bo!" from here select 'reate table in design view'.
You now need to create # fields for the database and select their data types.
$ield % needs to be called '&D'no' and have the data type of 'Auto(umber'. Also set this field as the
primary key.
$ield ) needs to be called '(ame' and have the data type of te!t.
$ield # needs to be called 'omments' and also has the data type of te!t" but this time you need to
change the default field si*e of +, to %,, characters under the '-eneral' tab in the '$ield .roperties'
bo! at the bottom of the screen.
/nce all the field's have been created and the data types and primary key set" save the table as
'tblomments'.
(ow the table has been created you need to enter some test data into the table. You can do this by
double0clicking on the new table 1tblomments2 in the main dialog bo!. $rom here you can enter
some test data. & would recommend entering at least # pieces of test data.
&f you are having trouble creating the database then you can download this tutorial containing the
Access Database with test data already entered.
Connecting to the Guestbook Database
(ow that the database is created and test data entered we can get on with creating the web page to
display the data from the database.
$irst we need to start web page" open up your favourite te!t editor and type the following 3TM4.
5html6
5head6
5title6My $irst A7. .age58title6
58head6
5body bgcolor9:white: te!t9:black:6
(e!t we can begin writing the A7. to connect to the database. $irst we need to create the variables
that we are going to use in the script.
5;
'Dimension variables
Dim adoon '3olds the Database onnection /b<ect
Dim rs-uestbook '3olds the recordset for the records in the database
Dim str7=4 '3olds the 7=4 >uery to >uery the database
(e!t we need to create a database connection ob<ect on the server using the AD/ Database
connection ob<ect.
'reate an AD/ connection ob<ect
7et adoon 9 7erver.reate/b<ect1:AD/DB.onnection:2
(ow we need to open a connection to the database. There are a couple of ways of doing this either by
using a system D7( or a D7(0less connection. $irst & am going to show you how to make a D7(0less
connection as this is faster and simpler to set up than a D7( connection.
To create a D7(0less connection to an Access database we need tell the connection ob<ect we created
above to open the database by telling the connection ob<ect to use the 'Microsoft Access Driver' to
open the database 'guestbook.mdb'.
You'll notice the A7. method '7erver.Map.ath' in font of the name of the database. This is used as we
need to get the physical path to the database. 7erver.Map.ath returns the physical path to the script"
e.g. 'c?@website@'" as long as the database is in the same folder as the script it now has the physical
path to the database and the database name.
'7et an active connection to the onnection ob<ect using a D7(0less connection
adoon./pen :.rovider9Microsoft.Aet./4BDB.C.,D Data 7ource9: E 7erver.Map.ath1:guestbook.mdb:2
&f on the other hand you want to use a slower D7( connection to the database then you will need to
replace the line above with the one below. Also if you don't know how to setup a system D7( you will
need to read my tutorial on" 7etting up a 7ystem D7(.
'7et an active connection to the onnection ob<ect using D7( connection
adoon./pen :D7(9guestbook:
(e!t create an AD/ recordset ob<ect which will hold the records from the database.
'reate an AD/ recordset ob<ect
7et rs-uestbook 9 7erver.reate/b<ect1:AD/DB.Fecordset:2
To >uery a database we need to use 7=4 17tructured =uery 4anguage2. &n the ne!t line we initialise
the variable 'str7=4' with an 7=4 >uery to read in the fields '(ame' and 'omments' form the
'tblomments' table.
'&nitialise the str7=4 variable with an 7=4 statement to >uery the database
str7=4 9 :7B4BT tblomments.(ame" tblomments.omments $F/M tblommentsD:
(ow we can open the recordset and run the 7=4 >uery on the database returning the results of the
>uery to the recordset.
'/pen the recordset with the 7=4 >uery
rs-uestbook./pen str7=4" adoon
Gsing a 'Do Hhile' loop we can loop through the recordset returned by the database while the
recordset is not at the end of file 1B/$2. The 'Fesponse.Hrite' method is used to output the recordset
to the web page. The 'Move(e!t' method of the recordset ob<ect is used to move to the ne!t record in
the recordset before looping back round to display the ne!t record.
'4oop through the recordset
Do Hhile not rs-uestbook.B/$
'Hrite the 3TM4 to display the current record in the recordset
Fesponse.Hrite 1:5br6:2
Fesponse.Hrite 1rs-uestbook1:(ame:22
Fesponse.Hrite 1:5br6:2
Fesponse.Hrite 1rs-uestbook1:omments:22
Fesponse.Hrite 1:5br6:2
'Move to the ne!t record in the recordset
rs-uestbook.Move(e!t
4oop
And finally we need to close the recordset" reset the server ob<ects" close the server side scripting tag"
and close the html tags.
'Feset server ob<ects
rs-uestbook.lose
7et rs-uestbook 9 (othing
7et adoon 9 (othing
;6
58body6
58html6
(ow call the file you have created 'guestbook.asp' and save it in the same directory folder as the
database" don't forget the '.asp' e!tension.
And that's about it" you have now created a connection to a database and displayed you -uestbook in
a web page" now to find out how add comments to the -uestbook through a web form read the ne!t
tuorial on" Adding Data to an Access Database.
&f you find that you are getting errors connecting to the database then please read through the Access
Database Brrors $A='s" practically make sure you have the correct '/DB Drivers' installed on your
system and if you are using the" '(T$7 file system'" make sure the permissions are correct for the
database and the directory the database in.

Você também pode gostar