ConnectionError: Failed to connect to 127.0.0.1:1433 - connect ECONNREFUSED

2,923 views
Skip to first unread message

Luiey

unread,
Mar 13, 2016, 11:04:41 PM3/13/16
to Node-RED
Hi,

I was just playing with node-red. Very new to write java. How to connect to mssql database?
Database installed: MSSQL 2008 Express.
Browser:Google Chrome

I already trying connect using Function and MSSQL nodes separately but both failed.
Function code:-
```
// Query with explicit connection
var sql = require('node-sqlserver');
var conn_str = "Driver={SQL Server Native Client 11.0};Server=(localhost);Database=smart_iot;Trusted_Connection={Yes}";

sql.open(conn_str, function (err, conn) {
    if (err) {
        msg.payload="Error opening the connection!";
        return;
    }
    conn.queryRaw("SELECT TOP 10 ID, str_name, dtm_date FROM dbo.tbl_supplier", function (err, results) {
        if (err) {
            msg.payload="Error running query!";
            return;
        }
        for (var i = 0; i < results.rows.length; i++) {
            msg.payload="FirstName: " + results.rows[i][0] + " LastName: " + results.rows[i][1];
        }
    });
});
return msg;
```

Nodes:-

Nodejs:-

Node Modules:-

Services:-

Note:- Already enable TCP/IP under SQL Server Configuration Manager

Julian Knight

unread,
Mar 14, 2016, 4:37:55 AM3/14/16
to Node-RED
Hi and welcome to the community.

BTW, it is JavaScript AKA ECMAscript not Java you are working with.

If there isn't a specific node type to connect to MS SQL Server and I don't believe there is at present (you can check at flows.nodered.org), you need to do the require statement in the settings file for node-red, instructions are in the docs. This is because the function node runs in a special virtual environment that restricts what you can do for safety.

So in your settings, you require the module to a node-red global variable & then you reference that variable in your function node instead of doing the require there. The rest of your code should then work OK.

Nicholas O'Leary

unread,
Mar 14, 2016, 4:59:04 AM3/14/16
to Node-RED Mailing List
Hi Luiey,

as Julian says, you cannot require modules directly in the Function node. The documentation for how to add a module via the global settings is here: http://nodered.org/docs/writing-functions#global-context - which includes an example of adding the 'os' module.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Luiey

unread,
Mar 15, 2016, 1:26:40 AM3/15/16
to Node-RED
Hi,

Yeah pardon my mistake. I just realize where I need to put those code inside modules folder not write it on function :-D
Now I can connect to the SQL Server. Btw, does anyone have link or guide on how to query SQL for insert and view(GET)?

Julian Knight

unread,
Mar 15, 2016, 2:36:19 PM3/15/16
to Node-RED
Hi Luley, no problem.

Not sure what version of MS SQL Server you are using but Microsoft's version of SQL is called Transactional-SQL or T-SQL for short:

Here are a couple of links to get you started.
Reply all
Reply to author
Forward
0 new messages