Reputation: 3674
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.
Upvotes: 17
Views: 25729
Reputation: 31
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
Upvotes: 0
Reputation: 1373
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.
Upvotes: 2
Reputation: 19334
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).
Upvotes: 7
Reputation: 1848
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
Upvotes: 2
Reputation: 89661
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;
Upvotes: 8
Reputation: 41
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.
Upvotes: 4
Reputation: 89661
Check out a new option:
https://github.com/orenmazor/node-tds
(from Node.js and Microsoft SQL Server)
Upvotes: 10