My Account Subscribe Help About
Sign In | Register FREE
Sunday, April 12, 2026
Iran chose 'not to accept our terms', US VP Vance says after negotiationsMore than 500 arrests at Palestine Action protestFaisal Islam: Why the government is relaxed about Chinese car imports'We need real peace': Easter truce fails to lift grim mood in war-torn UkraineWe spoke to the man making viral Lego-style AI videos for Iran. Experts say it's powerful propaganda'It's a special thing to be on Planet Earth': Artemis crew welcomed home in HoustonFrom blast off to splashdown: My days following Nasa's historic mission to the MoonBalamory is back - Miss Hoolie and PC Plum lift the lid on what to expectI've been a sex educator for six years. Why did I start doubting my contraception choices?The prophet and the mysterious death of Charmain SpeirsMessy and unpredictable: What I learned from election tour of the UKThe Papers: 'Le Humiliation' and 'US and Iran start historic peace talks'Hungarians decide whether to end 16 years of Orbán rule and elect rivalMcIlroy can't stop riding Augusta rollercoaster as final Masters round beckonsGolden eagles' return to English skies gets government backingScottish election 2026: How tax and welfare are shaping the voteCoachella kicks off with Sabrina Carpenter and surprise guestsI'm the only medic on the island – but I wish I'd brought some trousersGirl's 'mammoth' bone find may be 500,000 years oldCouple hitchhike to their wedding ceremonyThe construction boss who built a new life after three years in prisonBBC News appArtemis II: Return to the MoonUS-Iran Peace Talks + Artemis II ReturnsUnpack all the latest drama from Race Across the WorldHow young men's lives are influenced by the manosphereJuddering McIlroy simply can't stop riding Masters rollercoasterThe decade-long struggle to get AJ & Fury together'Big punch in the face' - could Arsenal really blow title from here?England far from perfect but Red Roses machine marches on
FDN » ASP & VBScript » Database Access with ADO

Database Access with ADO

Database Access with ADO

ADO (ActiveX Data Objects) provides a high-level interface for accessing data from ASP pages. This code sample demonstrates common database operations.

Connection and Query

<%@ Language="VBScript" %>
<%
Option Explicit

Dim conn, rs, strSQL

' Open connection
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=SQLSERVER01;" & _
    "Initial Catalog=FlamenetDB;Integrated Security=SSPI;"

' Execute a query
strSQL = "SELECT TOP 10 UserID, Username, Email, CreatedDate " & _
    "FROM Users WHERE IsActive = 1 ORDER BY CreatedDate DESC"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, conn, 3, 1  ' adOpenStatic, adLockReadOnly
%>

<table border="1" cellpadding="4">
  <tr>
    <th>User ID</th>
    <th>Username</th>
    <th>Email</th>
    <th>Joined</th>
  </tr>
  <% Do While Not rs.EOF %>
  <tr>
    <td><%= rs("UserID") %></td>
    <td><%= Server.HTMLEncode(rs("Username")) %></td>
    <td><%= Server.HTMLEncode(rs("Email")) %></td>
    <td><%= FormatDateTime(rs("CreatedDate"), 2) %></td>
  </tr>
  <% rs.MoveNext : Loop %>
</table>

<%
rs.Close : Set rs = Nothing
conn.Close : Set conn = Nothing
%>

Parameterized Query (Preventing SQL Injection)

<%
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = 1  ' adCmdText
cmd.CommandText = "SELECT * FROM Users WHERE Username = ? AND IsActive = 1"
cmd.Parameters.Append cmd.CreateParameter("@user", 200, 1, 50, strUsername)

Set rs = cmd.Execute()
If Not rs.EOF Then
    Response.Write "Found user: " & Server.HTMLEncode(rs("Username"))
Else
    Response.Write "User not found."
End If

rs.Close : Set rs = Nothing
Set cmd = Nothing
%>

Insert with Identity Retrieval

<%
Dim strInsertSQL, rsID, lngNewID
strInsertSQL = "INSERT INTO Posts (Title, Body, AuthorID) " & _
    "VALUES (?, ?, ?); SELECT SCOPE_IDENTITY() AS NewID"

Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = 1
cmd.CommandText = strInsertSQL
cmd.Parameters.Append cmd.CreateParameter("@title", 200, 1, 255, strTitle)
cmd.Parameters.Append cmd.CreateParameter("@body", 201, 1, -1, strBody)
cmd.Parameters.Append cmd.CreateParameter("@author", 3, 1, , lngAuthorID)

Set rsID = cmd.Execute()
Set rsID = rsID.NextRecordset  ' Skip to the SELECT result
lngNewID = rsID("NewID")
Response.Write "New post created with ID: " & lngNewID
%>
« Back to ASP & VBScript « Back to FDN