Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Senior Member Kings's Avatar
    Join Date
    Feb 2002
    Posts
    129
    Member #
    19
    Introduction
    One of the most common things in ASP, is to get data from a database. In this tutorial I'm going to show you how to get data from a database, and display it in a table.

    Let's make a connection first
    First we need to make a connection to our database.
    I'm going to make a DSN-Less connection, because many webhosts don't offer a DSN-connection.

    Code:
    <%@ Language=VBScript%>
    <% Option Explicit %>
    <!--#include file="adovbs.inc"-->
    <% Response.Expires = 0 %>
    
    <%
    'Declaring our variables
    Dim objConn, objRS, strQuery, strConnection
    
    'Create ADO Object
    Set objConn = Server.CreateObject("ADODB.Connection")
    
    'Our connection string, with the path to our database
    strConnection = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("yourdb.mdb")
    
    'Open up connection
    objConn.Open strConnection %>
    Now create a recordset
    We've opened up a connection to our database. Next thing we have to do is open up a recordset to select our data.

    Code:
    <%
    'Create our recordset
    Set objRS = Server.CreateObject("ADODB.Recordset")
    
    'Set our cursor types
    objRS.CursorLocation = adUseClient
    objRS.CursorType = adOpenStatic
    %>
    Select our data
    Now we've opened our connection, and the record set. We're ready to select the data from the database.

    Code:
    <%
    'Build up the query to select the data
    strQuery = "SELECT Row1, Row2 FROM Table1"
    'Select our data
    objRS.Open strQuery, objConn, , , adCmdText
    %>
    Display data
    Now we have selected our data, and it's ready to be displayed. We want to display it in a nicely formated table.

    Code:
    <table border="0" width="80%">
    <tr>
    <td width="40%">
    Row 1
    </td>
    <td width="40%">
    Row 2
    </td>
    </tr>
    <%
    'Let's print out our data
    'Do Until objRS.EOF = True
    While NOT objRS.EOF
    Response.Write("<tr>" & vbCrlf)
    Response.Write("<td width="40%">" & objRS("Row1") & "</td>" & vbCrlf)
    Response.Write("<td width="40%">" & objRS("Row2") & "</td>" & vbCrlf)
    Response.Write("</tr>" & vbCrlf)
    
    objRS.MoveNext
    Loop %>
    </table>
    Well, that was it!

    We first made a connection, then created a recordset, then selected our data, and finally printed it out in a nicely formatted table.
    K i n g s

    Several handy ASP Resources:
    4guysfromrolla | aspin | ASP101 | AspIt | learnasp

    Several handy ASP.NET resources:
    ASP.NET | .NET Experts | O'Reilly .NET | AspIt | 4guysfromrolla

    Several paid ASP/ASP.NET webhosts:
    Brinkster | Maximum ASP | Clicktech | Nodehosting | Uplinkearth | Webhost4life

  2.  

  3. #2
    Senior Member Kings's Avatar
    Join Date
    Feb 2002
    Posts
    129
    Member #
    19
    Here is adovbs.inc for the cursor types

    Because I can't attach .inc files, you need to rename the file from .txt to .inc

    Edit: sorry, attachments lost in the database transfer because the old host didn't have SSH --filburt1

    Edit: no problem, added attachment again --Kings
    K i n g s

    Several handy ASP Resources:
    4guysfromrolla | aspin | ASP101 | AspIt | learnasp

    Several handy ASP.NET resources:
    ASP.NET | .NET Experts | O'Reilly .NET | AspIt | 4guysfromrolla

    Several paid ASP/ASP.NET webhosts:
    Brinkster | Maximum ASP | Clicktech | Nodehosting | Uplinkearth | Webhost4life


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:10 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com