3

I am using Node JS with IIS (IISNode) and Microsoft's MS SQL driver package (MSNodeSQL) to create a http server. When accessing my database via the http server I get the following error when attempting to connect to the MS SQL database:

error { [Error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified] sqlstate: 'IM002', code: 0 }

But if I try to access the database locally on my server without the http server, then it connects perfectly fine. The ODBC driver is obviously installed.

Here is my code:

var http = require('http');
var sql = require('msnodesql');
var conn_str = "Driver={SQL Server Native Client 11.0}; Server=***; Database=***; UID=***; PWD=***;";

var blah = "";
http.createServer(function (req, res) {

    sql.open(conn_str, function (err, conn) {
        if (err) {
            console.log("error", err);
            res.end("error");
            return;
        }

        conn.queryRaw("SELECT * FROM ProductLinks", function (err, results) {
            if (err) {
                res.end('failed2');
                return;
            }
            for (var i = 0; i < results.rows.length; i++) {
                blah += "Id: " + results.rows[i][0] + " Name: " + results.rows[i][1] + " Text: " + results.rows[i][2] + " Link: " + results.rows[i][3];
            }
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.end(blah);
        });
    });
}).listen(process.env.PORT); 

I have a feeling it may be down to permissions somewhere since it works locally and not remotely, but I'm not entirely sure. Plus when locally, Node JS is being executed under a different user as opposed to IIS_IUSR which IISNode uses.

Any help would be great, thanks!

Tom O
  • 1,780
  • 2
  • 20
  • 43
  • I *believe* Node.js & msnodesql require SQL Native Client 11.0 driver to work (which is the one for MS SQL Server 2012). I don't believe it is possible to get msnodesql driver for Node.js to work with the ODBC SQL Server driver. it's listed as a requirement [npm msnodesql site here](https://npmjs.org/package/msnodesql) – Rachael Feb 07 '14 at 02:05
  • did you ever get this to work? – Rachael Feb 07 '14 at 02:05
  • I'm having a very similar issue communicating with an SQL Database running as an Azure service from a VM also in Azure. The same connection string works fine from my local machine to the cloud, but from the VM I'm getting the same error message as OP. I tried installing the driver as @displayname suggested, but with no luck. EDIT: I am not using IISNode. – smykes Feb 07 '14 at 16:23
  • @displayname I'm afraid I gave up with this idea, getting them both to work on Windows was a nightmare, I ran into so many issues. I eventually switched to a Linux server and everything ran smoothly with Node.JS. – Tom O Feb 10 '14 at 23:25
  • Good thinkin. I ended up rewriting my connection string and (really really annoying that it was so difficult to define). Did you end up using MSNodeSQL driver on your linux machine? – Rachael Mar 24 '14 at 23:16
  • And btw, that driver is a *THE B-WORD* to install. I had to use the MS installer (not git) to install it. It was pretty much impossible otherwise. – Rachael Mar 24 '14 at 23:18
  • 1
    Nah, I instead used Redis as a database instead of MS SQL - @Rachael – Tom O Mar 30 '14 at 03:58
  • I will have to check in to this Redis I keep hearing about. Unfortunately my company is tightly bound to MSSQL :( @ThomasOwers – Rachael Mar 31 '14 at 17:27
  • @Rachael That's a real shame, I love using NoSQL databases now, look into MongoDB too. – Tom O Apr 08 '14 at 14:28
  • 1
    When using Node with MSSQL, I advise NOT to use this module (https://github.com/Azure/node-sqlserver) because of the crazy list of prerequisites that you must install on your server. Give [node-mssql](https://github.com/patriksimek/node-mssql) a try. It uses [Tedious](https://github.com/pekim/tedious). Both pure Javascript solutions, blazingly fast because of V8! I'm using it with IISNODE, IIS 7.5 and MSSQL2008. Works great! – Christiaan Westerbeek May 13 '14 at 19:56

1 Answers1

0

I tried Node-MSSQL it's very good and very simple to use and it's connected and gets data successfully but it works on sql authentication only

Hany Hassan
  • 320
  • 2
  • 10