Sunday 23rd of November 2008

Example to read from a database

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 2 out of 5)
Loading ... Loading ... Posted on: April 3rd, 2008 by admin

Reading data from a database (MS Access/MySQL/SQL Server) is straight forward. The example below shows how to open a SQL statement, read the elements and display them.

var sSQL = “SELECT name FROM locations “; 
var rs = new ActiveXObject(”ADODB.Recordset”);
   
rs.Open(sSQL, dbSub);

while (!rs.EOF)
{
    var sName = String(rs.Fields(”name”).value).trim();

    Response.Write(”Name : “+sName+”<br>”);
    rs.moveNext();

}
rs.Close();
rs = null;

There are a few things to remember. When scrolling through a recordset you must ensure you move the marker to the next record (moveNext) otherwise you will get stuck in an endless loop.

Secondly, you must use the EOF marker to determine if the recordset is empty.

This example assumes that the database has been opened. See javascript DSN Database for more.

    Related posts

Leave a Reply

You must be logged in to post a comment.

Headlines

Feeds