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.
-
Were you finally able to accomplish this without building the proxy or using ActiveX?lo5– lo52011-02-18 20:04:31 +00:00Commented Feb 18, 2011 at 20:04
-
3I'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-tdsOren Mazor– Oren Mazor2011-02-28 15:19:25 +00:00Commented Feb 28, 2011 at 15:19
-
A pure javascript solution provides the node.js module tds4node. GitHub link: tdsttghr– ttghr2013-04-26 09:14:17 +00:00Commented Apr 26, 2013 at 9:14
-
Possible duplicate of Node.js and Microsoft SQL ServerZephyr– Zephyr2016-05-26 09:39:34 +00:00Commented May 26, 2016 at 9:39
7 Answers
Check out a new option:
1 Comment
*EXPERIMENTAL and currently ABANDONED*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
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
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
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
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