node.jsnode-mssql

Connecting to a remote Microsoft SQL server from Node.js


I was wondering if anyone was aware of a way to connect to a Microsoft SQL database from Node.js. I'm aware of the MySQL drivers, but I have data that I need to pull from a MS SQL database and would rather pull directly from Node.js rather than hack a PHP script of some sort in place.


Solution

  • I suspect you'll have to wrap your SQL Server with a JSON outputting web-service. On the positive side, it should be relatively easy to do.

    Be nice if the JavaScript engine in node.js could do this: (from How to connect to SQL Server database from JavaScript in the browser?):

    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;