Connecting to a remote Microsoft SQL server from Node.js
-
12-10-2019 - |
Pergunta
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.
Solução
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;
Outras dicas
Check out a new option:
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).
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.
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
Just today I released a new module, for windows only, allowing native and asynchronous use of MSSQL. It's called TSQLFTW, and currently supports connecting and querying the database. It returns results in JSON.
Check out the Github here: https://github.com/gfosco/tsqlftw
Hacker News submission/comments: http://news.ycombinator.com/item?id=3353389