|
|
|
|
|
|
Printer Friendly Version
Part
1: Creating the database and reading from it Firstly, let me
outline what this tutorial will do. This is a practical guide, which will show
you how to put together an intranet system, using ASP. This system will be easy
to use for the complete novice, and simple to administer. This guide provides
a basic outline: you will have to customize it to your needs. That said, let's
get going. These are the main features of our system: For this tutorial,
I assume that you're using Microsoft Internet Information Server or Microsoft
Personal Webserver. If you're an NT user and you don't have IIS, then you can
download it from microsoft.com as part of the NT Option Pack. I promised a practical
guide, so let's get practical. Step
1: Putting the database together All of the information
on the Intranet site, such as articles, will be stored in a database. This will
provide a central location for information, making the site easier to back-up
and organize. Consider your needs for the database. Below is a sample table: | ID | Title
| Author | Department | Date | Body |Enabled | And here are the
values for one article: | 01 | Request
| Peter | HR | 03/07/2001 | Give me money!! | Yes | So, the 'ID' column
contains a unique reference number, and 'title', 'author' and 'department' are
text values. 'Date' obviously contains the date, 'body' contains the article
and 'enabled' lets the administrator enable or disable a particular article. Draw out the columns
you need for your site, and using software such as Microsoft Access, create
a database. There should be a main table, which must contain the field entitled
'ID'. See below: 'ID': Set
to 'autoincrement', 'long', 'unique', and 'primary key'. For the other
Fields: Text values:
Anything needing text values under 200 characters, set the datatype to 'text'. Add a sample record
using Access, like I've done above, and save your database in MDB format. Put
this in a folder called 'databases' within the folder used by your web server,
usually called WWWroot or Inetpub. Step
2: The Interface: Creating a page to list all records in the database The first step
of the interface will be to to read articles from the database, and show them
to the user. The code below will do this, and is fully annotated. NOTE: I advise
you understand how this code works before you use it, by reading the accompanying
notes, in case anything goes wrong. If you don't understand it all, don't panic! Dim
dblocat, cnn, cnnstring, rs, SQL 'The location
of our database is stored in the variable called dblocat 'These 2 lines
tell the server how to connect to the database. set
cnn = server.CreateObject("ADODB.Connection") 'Now we've told
it how to connect, this says to the server "Go Connect!" cnn.open(cnnstring) 'Right, we're
doing well. The database is open, ready and waiting. Now, let's Set
rs = server.CreateObject("ADODB.Recordset") 'Now we can
query the database. The following line will do this: SQL=
"SELECT * From departmental" 'The stament
above should be written in the format: 'SELECT * From tablename' rs.Open
SQL 'Now, print
the results on the screen. The HTML Tags, below, will put the data into a simple
table. rs.MoveFirst And you're done!
This page will produce the following table: As you can see,
I added 3 sample records to the database, which were listed in turn. With regards to
how things were kept in tabular form, typing <tr>
will start a new row and </tr> will end it.
Everything in between these two "tags" goes in that row. Typing <td>
will start a cell within a row, and </td>
will close it. Using these four tags, you can create a table with multiple rows
and columns. For more information, get a book on HTML or download and examine
the code example at the bottom of the page. Using this code,
you can keep a central source of information that any page can read from. However,
this displays all of the records; what you really want is to be selective with
what you show. That will be covered in the next tutorial. Download the
code and database: Lesson1.asp
(todo:
edit the line specifying the database path (line 8) ) Intranet.mdb
(todo:
Put it in a sub-folder called data underneath the ASP file) Next time: Further reading: |
| |
|
· Intranet eXchange Discussion Board · Advice and Opinions |
Intranet Journal's Tutorials |
|
Managing Editor |