CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Miscellaneous] ASP Database Connectivity


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author Rae On 2007-04-29 10:02:29




View and vote on the article here: ASP Database Connectivity


ASP Database Connectivity

Category
Miscellaneous
Summary
Body
ASP Database Connectivity


This article teaches you about connecting your Active Server Pages (ASP) to a database. I'm assuming that you are already familiar with basic ASP programming techniques and database concepts. First of all, setup your basic development environment. This means, install Microsoft Windows, a web server which supports ASP and a database. Since we are using Microsoft technologies all the way, let's choose Internet Information Server (IIS) or Personal Web Server (PWS) as our ASP-supporting web server and Microsoft Access as the database, which comes bundled with the Microsoft Office suite.
So let us start building our little ASP database connectivity example. First, open up Microsoft Access from the start menu and make a blank database. Save this database somewhere, and call it 'student.mdb', since the database will contain information about students, so it makes sense to give it a descriptive name. Now since this is not a tutorial on Microsoft Access, and also the fact that Access is pretty easy to learn and use, I'll assume that you are capable of creating a table and its fields. Name your table as 'InfoTable', and make two fields in it, one 'Name', which will be of variable character type, and the second 'Roll', which will be of integer type. Now enter some dummy record values into this table and finally save it. This finishes our database part of the whole process.
The next step involves making an ODBC connection. ODBC stands for Open DataBase Connectivity, and is a set of vendor independent drivers to connect databases to application programming languages. To make the connection, go to the control panel and click on the icon called '32 Bit ODBC' if you are using Microsoft Windows Me/9x. If you are using Windows XP, go to 'Administrative Tools' and then 'Data Sources (ODBC)'. In the window that opens up, click on the 'File DSN' tab on the top. In case you are wondering what DNS stands for, its 'Data Source Name'. Now click 'Add' and then select "Microsoft Access Driver (*.mdb)". Save this DSN somewhere on your hard disk, give it a descriptive name like 'trialdsn.dsn'. Then all that is left in this step is to select the database to which you'd like to connect to using this DSN. Select the Access database that you created earlier in this article.
The last step in the process is the real ASP coding. Remember that ASP is actually a server side scripting language, similar to PHP in its design goals. Now, I'll be using VBScript for coding the ASP pages. The following piece of code is used for setting up the connection using the data source name we created earlier.

<tt>
&lt;%

Set MyConn = Server.CreateObject("ADODB.Connection")

MyConn.Open "FILEDSN=c:trialdsn.dsn"

%&gt;
</tt>

Now we'll see how to perform some routine operation on this database and display the result in the web browser using ASP. We use Structured Query Language (SQL) to perform operations on the database. This is achieved by the following piece of code,

<tt>
&lt;%

SQL_query = "SELECT * FROM InfoTable"

Set RS = MyConn.Execute(SQL_query)

%&gt;
</tt>

Here you'll see that the result of the executed query is stored in 'RS'. Its stands for 'Record Set' and is nothing but an array holding all the data we selected from the table. To display the records on a web page, we loop the recordset till there are no more records left. This particular condition is checked using 'EOF', which signifies End of Stream. To close the connection after all operations are done, first close the recordset using 'RS.Close', then finally close the database connection using 'MyConn.Close'. The code for displaying is shown below,

<tt>
&lt;%

WHILE NOT RS.EOF

%&gt;

&lt;LI&gt;&lt;%=RS("Name")%&gt; : &lt;%=RS("Roll")%&gt;

&lt;%

RS.MoveNext WEND

%&gt;
</tt>

&nbsp;
References
<a href>http://www.haneng.com/[/url]

<a href>http://www.programmersheaven.com/[/url]

<a href>http://www.planetsourcecode.com/[/url]


This article was imported from the CyberArmy University site. (original author: rae)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.03594 seconds


About Us | Privacy Policy | Mission Statement | Help