17

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.

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
A Wizard Did It
  • 3,614
  • 4
  • 28
  • 32
  • Were you finally able to accomplish this without building the proxy or using ActiveX? – lo5 Feb 18 '11 at 20:04
  • 3
    I'm kind of glad I just came across this because I was wondering if anybody would be interested in my node.js-mssql project. It's highly rudimentary at the moment, but I'll be doing another big push later this week with more functionality :) https://github.com/orenmazor/node-tds – Oren Mazor Feb 28 '11 at 15:19
  • A pure javascript solution provides the node.js module [tds4node](https://npmjs.org/package/tds4node). GitHub link: [tds](https://github.com/ttghr/tds) – ttghr Apr 26 '13 at 09:14
  • Possible duplicate of [Node.js and Microsoft SQL Server](http://stackoverflow.com/questions/5156806/node-js-and-microsoft-sql-server) – Zephyr was a Friend of Mine May 26 '16 at 09:39

7 Answers7

10

Check out a new option:

https://github.com/orenmazor/node-tds

(from Node.js and Microsoft SQL Server)

Community
  • 1
  • 1
Cade Roux
  • 88,164
  • 40
  • 182
  • 265
8

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;
Community
  • 1
  • 1
Cade Roux
  • 88,164
  • 40
  • 182
  • 265
  • 3
    Should I downvote this answer, because this is not the best answer anymore in 2014? I'm not sure if downvoting an obsolete answer is considered morally permissible. Look here for the what I think should be used today: http://stackoverflow.com/questions/5156806/node-js-and-microsoft-sql-server/22658512#22658512 – Christiaan Westerbeek May 13 '14 at 20:16
  • 2
    This is no longer a good answer in 2015. [Sequelize](https://github.com/sequelize/sequelize) (if you want ORM) or [Tedious](https://github.com/pekim/tedious) are much better options. – treythomas123 Sep 24 '15 at 15:24
7

I would recommend node-mssql, which is a nice wrapper for other connectors, the default being my previous choice (Tedious) bringing a bit nicer of an interface. This is a JavaScript implimentation, with no compilation requirements, meaning you can work in windows and non-windows environments alike.

Another option, if you don't mind bringing in .Net or Mono with a binary bridge would be to use edge.js. Which can be very nice if you want to leverage .Net libraries in node.js

node-tds is abandoned, node-odbc doesn't work with windows, and the MS node-sqlserver driver doesn't seem to work on non-windows (and has some goofy requirements).

Tracker1
  • 19,103
  • 12
  • 80
  • 106
4

If you are connecting to Mssql from linux you can use node-odbc ( https://github.com/w1nk/node-odbc ) with the freetds odbc driver. I am using this in production and its faster than wrapping a web service.

Doug Porter
  • 7,721
  • 4
  • 40
  • 55
wankdanker
  • 41
  • 1
  • 3
2

Another option, from Microsoft even,

http://www.microsoft.com/en-us/download/details.aspx?id=29995

Or a linux sql client driver via odbc:

http://www.microsoft.com/en-us/download/details.aspx?id=28160

Sneaky Wombat
  • 1,838
  • 2
  • 21
  • 29
  • 1
    The MS option requires a binary module build, and appears to be windows only. – Tracker1 Dec 04 '12 at 22:11
  • there are a lot of ways to do this. the microsoft driver is just one. I actually don't use it though for the same reason, windows only. :( I've just found this today though... http://www.microsoft.com/en-us/download/details.aspx?id=28160 – Sneaky Wombat Dec 07 '12 at 19:04
2

New answer for 2015: The ORM package Sequelize now supports MS SQL, using the Tedious driver under the covers.

This is the best way I've found to interact with Microsoft SQL Server.

treythomas123
  • 1,373
  • 1
  • 11
  • 11
0

I recently encountered this problem, I was trying to connect the MSSQL that is hosted on a remote server. The config that I had to use is-

let config = {
    user: 'user',
    password: 'password',
    server: 'server',
    database: 'database',
    "options":{
        instanceName: 'instanceName',
        "encrypt":true,
        "enableArithAbort":true,
        "trustServerCertificate": true,
       }
};
module.exports=config;

For getting the instance name use SELECT @@servicename in SSMS