Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to SQL Server with Javascript

Status
Not open for further replies.

LilProgrammerGirl

Programmer
Jun 24, 2004
81
US
Hi all,
I have been searching and searching... but I cannot find a way to connect to a SQL Server via Javascript code. Is this even possible??

Thanks,
HaileyMarie
 
vis JS? unless you're in an asp page written in JS, or using some godly remote connection code, no you cant.

[thumbsup2]DreX
aKa - Robert
 
forgot to mention, if you are in an asp page :

thread216-880056

[thumbsup2]DreX
aKa - Robert
 
:-(
I have an ASP page that links to my SQL Server (connects to SQL Server okay via ASP) to display a record. Then, I have 3 buttons below the record that do different functions when you click on them. One just goes back to the main page, one populates an email with the page contents, and one is supposed to reconnect to the sql server and move a record from one table to another via insert and delete.

I have programmed the first two buttons and they work beautifully. Now my problem is still when you hit the last button. I would like it to connect and do an insert query and then a delete query.

Does anyone have advise as to how I should accomplish this?

Thanks much!
HaileyMarie
 
done exactly the same as the first two, just handle the insert query instead of a select query, and recommendation on the deletions, if you have some form of "common" unique id, do a delete form table 1 where idfield in (select idfield from table2)

that way you only delete matching records, just incase the insert fails, but be careful, if you have a non-unique idfield you'll kill more than you bargained for.

[thumbsup2]DreX
aKa - Robert
 
I appreciate the comment on how I have to be EXTREMELY careful about the delete - thank you for mentioning... however, I am still confused as to how I handle my third button like the others. The first two buttons didn't have to connect to the database at all. They are all calling JS Functions. How can I perform the insert and delete in JS?? (Thank goodness for these forums...) Do I have it open another sheet in ASP, send the sql statement as a variable, and connect to the database in ASP?

Much appreciated!
Hails
 
well the first buttons are editing right? or at least displaying data aren't they?

don't know havent been following the dupe thread.

[thumbsup2]DreX
aKa - Robert
 
You can't connect to the DB from JS. Like you say, the 3rd button should be a form submission to another ASP on the server. This ASP should do all the work.

Greg.
 
Hi guys,

There is a conception that you cannot connect to your database using javascript. but i believe that if you try using ActiveX controls it is a cake walk. The security is still an issue but if you wish to connect using client side scripting javascript is the way to go.

var conn
var rs

conn = new ActiveXObject("ADODB.Connection");
conn.open("dbdsn");
rs = new ActiveXObject("ADODB.Recordset");
rs.open("Select * from temp")
while (!rs.eof)
{
alert(rs(0));
rs.movenext;
}
rs.close;
conn.close;

Enjoy :)

 
There is a conception that you cannot connect to your database using javascript.

For all of you who think that using ActiveX controls will solve you problems, remember that ActiveX controls ONLY WORK WITH AN IE browser. Also understand that the first time a user visits the page, they will get a warning message to the effect that the ActiveX control being used may not be a TRUSTED control.

All of this raises a couple of concerns:

1 - are you willing to limit your visitors to just those who use IE browsers (approx. 80% of the market).

2 - whenever the BROWSER warns the user of a possible flaw or danger (in this case, an untrusted ActiveX object), you tend to scare the user away from using your site.


IMHO, it's far better to find another method that can be used by all browsers than to limit yourself, and your users, to a single browser. As one Tek-Tips member has said many times - "design for the web, not the browser".



There's always a better way. The fun is trying to find it!
 
Hi all,
Well, althought javascript is not natively made to make connections to a server (for doing SQL or for any other kind of use), I have found great trick to make this work.
This uses Javascript ONLY and is cross-browser (at least on the recent ones).

In my latest Proof-of-concept, I successfully get a query from a textarea, send it to my MySQL server, and display the results in a table, all of this without reloading the page.

To send the query, I'm updating (via JS+DOM) the SRC parameter of an empty <script> tag, with the query as parameter of the URL.
On the other side, I have a web server, running PHP, which returns the results directly as JS code (This js code is in fact just the assignation of a JS array)
Updating the table is done using DOM.
You can really do some funny stuff with this, especially because the response is coming back asynchronously (thus your JS code on the client side isn't blocked).
 
ActiveX is inherently insecure (because it's really tightly coupled to the OS), and mayor security firms, and even Microsoft itself, propose that you deactivate it for sites you don't know. In addition to the browser support problem.

haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top