Jul 022011
 

Hi All,

I have just worked up a short script to search a SQL server using Javascript.
Using ActiveX and the ADoDB.Connection object this enables me to build a little HTA frontend to do what I need.

Firstly, we create the ADODB object.

var objConnection = new ActiveXObject("adodb.connection");

Then, set the options we want for the connection.
I’m connecting to a SQL server, that is hosted on “server1”, and the database i want is “db1”, and I’m connecting with username and passwords.

var strConn = "driver={sql server};server=server1;database=db1;uid=username;password=password";

Then we open the connection with the options specified.

objConnection.Open(strConn);

Once the connection has been opened, we use the ADODB.Recordset object to execute a query and retrieve results from SQL.


var rs = new ActiveXObject("ADODB.Recordset");
var strQuery = "SELECT * FROM users WHERE username = 'user1'";
rs.Open(strQuery,objConnection);

Once the query has been executed, the results will be stored in the recordset.
You will now be able to view the results using the standard recordset functions.


rs.MoveFirst
while(!rs.eof) {
//Do your thing here
rs.movenext;
}

Share

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.