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.

4
  • Were you finally able to accomplish this without building the proxy or using ActiveX? Commented Feb 18, 2011 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 :) github.com/orenmazor/node-tds Commented Feb 28, 2011 at 15:19
  • A pure javascript solution provides the node.js module tds4node. GitHub link: tds Commented Apr 26, 2013 at 9:14
  • Possible duplicate of Node.js and Microsoft SQL Server Commented May 26, 2016 at 9:39

7 Answers 7

10

Check out a new option:

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

(from Node.js and Microsoft SQL Server)

Sign up to request clarification or add additional context in comments.

1 Comment

The node-tds project description now reads *EXPERIMENTAL and currently ABANDONED*
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;

2 Comments

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: stackoverflow.com/questions/5156806/…
This is no longer a good answer in 2015. Sequelize (if you want ORM) or Tedious are much better options.
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).

Comments

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.

Comments

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

2 Comments

The MS option requires a binary module build, and appears to be windows only.
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... microsoft.com/en-us/download/details.aspx?id=28160
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.