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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

serverside javascript to do a database connection 1

Status
Not open for further replies.

brianpercival

Programmer
Jun 13, 2005
63
US
hi,

I am new to serverside javascript, being a coldfusion developer. Can anyone please point me to a complete example where I can use javascript to connect to a database and query and thereby manipulate the result set?

regards,
Brian
 
When you say serverside javascript I assume you mean ASP using JScript. If that is the case, this is how I do it:

You will have to fill in the *'s with your own information. Data Source is the name of your server, Initial Catalog is the name of the database, User Id and Password are self explanatory.
Code:
var strConn = "Provider=SQLOLEDB;Data Source=*;Initial Catalog=*;User Id=*;Password=*";

var oConn = Server.CreateObject("ADODB.Connection");
oConn.Open(strConn);
var oRS = Server.CreateObject("ADODB.Recordset");
oRS.ActiveConnection = oConn;
oRS.CursorType = 3;
oRS.LockType = 3;
oRS.CursorLocation = 3;

oRS.Source = "SELECT BLAH FROM SOMETABLE WHERE SOMETHING = SOMETHINGELSE";
oRS.Open();
while (!oRS.EOF) {
   Response.Write(oRS.Fields("BLAH").value + "<br>");
   oRS.MoveNext;
}
oRS.Close();

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
kaht,

I see what u do, but in the application I am looking at, it seems the severside script is not necessarily jscript. I see a ASP file containing a javascript code fragment like
<script runat="server" language="javascript">
.....
</script>
the file has nothing else. this script code has all db related functions like executeSQL,insertRecord etc etc but I couldn't understand how he ismakingthe db connection in the first place. The asp file is included in all other files where a db interaction is needed. So I am not sure if there is a third party module that needs to be installed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top