|
|
Title |
Reading database with ADO and VBScripts
|
Summary |
This note shows simple methods to add records to a database using the ADO interface with VBScript. |
Contributor |
John McTainsh
|
Published |
18-Oct-2000 |
Last updated |
18-Oct-2000 |
|
|
Download demo ASP and MDB files - 12 Kb
Description.
The Microsoft¢ÎActiveX¢ÎData Objects (ADO) object model defines a collection of programmable
objects that can be used in any language that supports both COM and Automation to access Databases. In
this example we will be using ADO to access a Microsoft Access Database.
Note: I have been working on ADO for 5 days now and this is what I have learnt in this
time. Also remember you need Microsoft IIS running on the server since this is all
server side processing.
How to use it.
Start as always by inserting <%@ LANGUAGE = VBScript %> at the top of the page.
The file I will be working with is called TestADO.mdb . When setting these demos
up for yourself de sure to change the DBQ=D:\Inetpub\DevelopmentPrivate\TestADO.mdb; path
to your local path.
Read data from the database. (View recordes)
The important code in this block is where the record is read from the database.
Click here to view the results.
'Create and open connection to the data source
strProvider = "Driver={Microsoft Access Driver (*.mdb)};" &_
"DBQ=D:\Inetpub\Development\ASP\TestADO.mdb;"
'Setup the match query
strQuery = "SELECT * FROM Comment;"
'Instantiate a Recordset object and open a recordset using the Open method
Set rst = Server.CreateObject("ADODB.recordset")
rst.Open strQuery, strProvider
ON ERROR RESUME NEXT
While ( not rst.EOF )
strAuthor = rst( "Author" )
nAge = rst( "Age" )
strMessage = rst( "Message" )
dateLastRefreshed = rst( "LastRefreshed" )
nDay = Day( dateLastRefreshed )
nMonth = Month( dateLastRefreshed )
nYear = Year( dateLastRefreshed )
%>
<tr>
<td><%=strAuthor%></td>
<td><%=nAge%></td>
<td><%=strMessage%></td>
<td><%=nDay%>-<%=nMonth%><%=nYear%></td>
</tr>
<%
rst.MoveNext
Wend
What needs further investigation.
We are not doing any error handling. If any one knows how to do this add it to the comments.
|