Important alert: (current site time 7/15/2013 12:44:34 PM EDT)
 

VB icon

ADO With JavaScript

Email
Submitted on: 4/12/2002 5:37:25 PM
By: Marcio Coelho 
Level: Intermediate
User Rating: By 21 Users
Compatibility: JavaScript
Views: 44682
author picture
(About the author)
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' 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.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2804&lngWId=14'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>


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/15/2002 9:42:53 AMRasputin

Cool Marcio!!

4 Globes!

Thanks,
-Ras
(If this comment was disrespectful, please report it.)

 
4/16/2002 2:37:10 PMAndres

Yes it is that i was looking for.
Excelent.

(If this comment was disrespectful, please report it.)

 
5/1/2002 10:31:48 PMMoe

I tried the sample and changed the connection to link a MS Access 97 database but ran into errors. Do you have an example to help.

Thanks,
(If this comment was disrespectful, please report it.)

 
5/2/2002 9:27:46 AMMarcio Coelho

'for access users
(If this comment was disrespectful, please report it.)

 
6/13/2002 7:32:29 AMDrew

Thanks for posting. Continually searching for ways to avoid webform refresh.
But exposed password is an issue. Can is used trusted?
(If this comment was disrespectful, please report it.)

 
6/14/2002 9:19:27 AMBob Jones

I see it in Access how about Oracle?
Please!
(If this comment was disrespectful, please report it.)

 
6/14/2002 9:29:53 AMMarcio Coelho

Hi Bob,

I wish I could answer you right now, but I have no Oracle database to test any sample and I send that to you.

I can not asnwer you on that, but look, the connection string should be any oracle connection string.

You can do that in different ways, but for sure you can create a DSN(ODBC) using your control panel. if you need help on that, I can send you the steps.

Then, you need to get the ORACLE SQL, that I believe is very similar to SQL Server or Access SQL.

The rest is the same.

'*****************

Again, the only difference will be the connection string and I am sure Planet-source-code has plenty samples for oracle connection using DSN.

Second, there may be a little change on the SQL, minor thing. You should look for oracle SQL.

AS far as the javaxcript sysntax and the ADO, there will be no change on that.

Marcio
(If this comment was disrespectful, please report it.)

 
6/18/2002 11:28:44 AMAaron

Just what I needed. I write most of my connections in ASP(VB), I connect to many different DB in our intranet(DB2, SQL, Lotus Notes, etc) but there are times that I need to connect in JScript. Thanx
(If this comment was disrespectful, please report it.)

 
7/4/2002 1:43:28 AMSteve

I'm using the script for MS-Access and it tries to connect to the DB on the machine browsing the site, not the DB on the webserver. What did I miss?

Thanks,
(If this comment was disrespectful, please report it.)

 
7/5/2002 8:27:43 AMMarcio Coelho

Hello Steve,

I believe you did not miss anything.
This is a client script and client scripts run on the user machine.

You can add a tag to run the script on the server side, but then if you run on the server side you may be able to use asp itself.

you can still change the location of the msaccess to run on the client but then you need to have security on that server and also on the mdb file since you are given away its address and letting the users to see it from the view source.

Marcio
(If this comment was disrespectful, please report it.)

 
7/23/2002 11:32:38 AMMariel

It's very useful !
How can I avoid the message
(If this comment was disrespectful, please report it.)

 
7/23/2002 11:37:47 AMMarcio Coelho

Just take out the sentence with alert(..) since alert is the way javascript shows a message.
If you prefer you can comment it with // before it.
(If this comment was disrespectful, please report it.)

 
7/23/2002 11:52:51 AMMariel

I mean the msg when opening the conection: This page is accessing a data source on another domain ...
(If this comment was disrespectful, please report it.)

 
7/23/2002 1:09:15 PMMarcio Coelho

You need to change the connection string to represent server and database under your own domain AND you need to add the domain name in the trusted sites
under your internet security settings.
It is a browser security issue.
You should make changes on Tools\Internet Options\Security\Trusted Sites
(If this comment was disrespectful, please report it.)

 
9/12/2002 6:28:03 AM

I tested this code on MS-Access XP and 2000 but it generates error (Couldnot find Installable ISAM). Operating system is 2000 Professional. How could I overcome this problem
(If this comment was disrespectful, please report it.)

 
2/12/2003 4:33:51 PM

Great code and even greater update on the MS Access db. I see in the MS Access db response, that the path to the db is an absolute one, does this still require you to register the Access db? I'm looking for a solution that will not require the client to register the db on their system in order to access it.
(If this comment was disrespectful, please report it.)

 
2/12/2003 6:26:32 PM

How do you display the info with javascript? thanks.
(If this comment was disrespectful, please report it.)

 
2/20/2003 12:48:54 PM

What SQL statements can you use with this? Can up update/add/delete to the db with JS? What are the syntax cause I can't find anything related to JS and DB access. Can you call up stored precedures with JS?
(If this comment was disrespectful, please report it.)

 
1/9/2004 7:27:38 AM

Marcio what a simple and nifty piece of code. Congratulations
(If this comment was disrespectful, please report it.)

 
2/11/2004 8:37:52 AM

Hi, I'm trying to access the last record on my query with "moveLast" but there was an error..., I try this code:
while(!rs.eof)
{
rs.moveLast
alert(rs(0));
}
Is there a problem? Thanks a lot!!!
(If this comment was disrespectful, please report it.)

 
2/11/2004 4:28:50 PM

Awesome coding, especially the part about Access. However when using the INSERT I get
(If this comment was disrespectful, please report it.)

 
2/11/2004 4:30:54 PM

Awesome coding, especially the part
about Access. However when using the
INSERT I get Current Recordset does not support updating. This may be a limitation of the provider or of the selected locktype.

I'm running across a WinXP LAN
(If this comment was disrespectful, please report it.)

 
3/18/2004 11:24:54 AMAurelio Centini Junior

Parabéns, o seu código quebrou o maior galho e abriu um grande leque de aplicações para mim. na verdade eu usei o seu código para criar uma instância de uma DLL no servidor e usei os metodos da mesma para acessar o banco de dados.
Um grande abraço
(If this comment was disrespectful, please report it.)

 
3/22/2004 6:26:57 AM

Hi ..
I have tried running this on Apache Tomcat Server ... but it can't link to the database file ...
what should i do?
(If this comment was disrespectful, please report it.)

 
4/10/2004 4:46:58 AM

Hi, thanks for ur help.
One more question for u.

If I wanna use a variable in where clause of SQL. then how it should be written.
Here is the examp:

function Load()
{
sid = 4 ;
var conn = new ActiveXObject("ADODB.Connection") ;
conn.Open("dsn=cti_samir;uid=sa;pwd=;");
rs= conn.Execute("select agent_name from agents where agent_id= '"&sid&'"
while(!rs.eof)
{
alert("done");
alert(rs("agent_name"));
rs.movenext
}
rs.close
conn.close
}

It won't work as VbScript '"&sid'"&
Plz. help in finding what should be here.

Thanks.
(If this comment was disrespectful, please report it.)

 
4/12/2004 10:02:48 PM

Hey Marcio,

I have a question.

This is very useful code and works well for me using IE. However, when using Netscape Navigator 7.1 I receive the following message reported in the JavaScript Console -
Error: ActiveXObject is not defined

The Mozilla ActiveX control and plugin support is enabled in the Netscape browser.

Any advice about how to resolve this error is greatly appreciated
(If this comment was disrespectful, please report it.)

 
5/5/2004 8:02:50 AMBonj2

Doesn't work. No idea why.
(If this comment was disrespectful, please report it.)

 
9/24/2004 1:56:53 AM

in VBScript it will be like this
'"&sid&"'
in JavaScript it will be like this
'"+sid+"'

is there a solution to avoid the popup dialog telling a script trying to access tru my SQL2000? its irritates me..
(If this comment was disrespectful, please report it.)

 
11/18/2004 4:37:54 AM

var conObj = new ActiveXObject(
(If this comment was disrespectful, please report it.)

 
11/18/2004 4:39:37 AM

var conObj = new ActiveXObject("ADODB.Connection") ;
var connectionString = "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=orcl;uid=your-username;pwd=your-password;"
//here orcl is your Oracle alias
conObj.Open(connectionString);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT testno, address FROM testtableam", conObj);
while(!rs.eof)


{
alert(rs(0));
rs.movenext;
}

rs.close;
conObj.close;
(If this comment was disrespectful, please report it.)

 
12/3/2004 1:19:49 PM

Hi, I want to retrieve nearly 100,000 records from Active Directory. Can you please get back to me with some source code for retrieving it.

Thanks in advance

raj d.s
(If this comment was disrespectful, please report it.)

 
3/28/2005 6:18:31 AM

Great, this is what I've been searching for all this time. Thanks Marcio Coelho.
(If this comment was disrespectful, please report it.)

 
4/7/2005 12:13:07 PM

How do u store data from forms to table access database
(If this comment was disrespectful, please report it.)

 
10/25/2008 8:05:12 PMwahyu

a have a question, how to create automatic path, connection ADO with java script. i have experience in VB programming to create connection with ADO
use syntak "'& AppPath &'". thank for the answer.
(If this comment was disrespectful, please report it.)

 
7/23/2009 7:17:13 AML-SUM

Hi..
Thanks it is very helpfull
(If this comment was disrespectful, please report it.)

 
8/24/2009 5:57:49 PMDan

Do I need to be using ASP.net for this to work correct or will a simple client side javascript work?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.