Mahek Merchant Mahek Merchant - 10 months ago 56
SQL Question

Can we connect database with javascript for functional testing?

I made my RestAPI service, now i want to validate responses with my database using Javascript for functional testing.Is it possible to do so?

Answer Source

You shouldnĀ“t use client javascript to access databases for several reasons (bad practice, security issues, etc) but if you really want to do this, here is an example:

var connection = new ActiveXObject("ADODB.Connection") ;

var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";

connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");

rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
   document.write(rs.fields(1));
   rs.movenext;
}

rs.close;
connection.close;