Monday, May 12, 2008

How to access a database by ADO with JavaScript

I have not found any sample that shows database access with javaScript. Well, I created one. It is very simple since Javascript has the activeXObject that allow us to call an external object in the same way that vbscript uses CreateObject.
//**************************************
// Name: ADO With JavaScript
// Description:I have not found any sample that shows database access with javaScript. Well, I created one.
// It is very simple since Javascript has the activeXObject that allow us to call an external object
// in the same way that vbscript uses CreateObject.
// By: Marcio Coelho
//
//Assumes:You need SQL Server, a valid login, and know the name of a table and at least one column to test the sample.
//
//Side Effects:None
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.2804/lngWId.2/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************

<SCRIPT LANGUAGE=javascript>
 var conn = new ActiveXObject("ADODB.Connection") ;
 var connectionstring = "Provider=sqloledb; Data Source=itdev; Initial Catalog=pubs; User ID=sa;Password=yourpassword"
 conn.Open(connectionstring)
 var rs = new ActiveXObject("ADODB.Recordset")
 rs.Open("SELECT gif, description, page, location FROM menu1", conn)
// Be aware that I am selecting from this table, but you need to pick your own table
 while(!rs.eof)
  {
    alert(rs(0));
    rs.movenext
  }
   
   rs.close
  conn.close 
</SCRIPT>
 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory