How to read data from a database using javascript
Posted on: April 5th, 2008 by adminReading 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.

