简体   繁体   中英

Problems in connecting with SQL Server using node js?

I am trying to connect to a remote sql server database which is hosted in a remote server using node js. Now, I tried to connect to the same database using c# and everything works perfect but I tried the same thing with Node and I keep getting error logs. I am unable to understand if the problem is at my end or is it the problem with the server.

var sql = require('mssql');

var config = {
    userName: 'realm',
    password: 'friend',
    server: '172.10.3.22',

    options: {

        database: 'ElmaTest',
        instancename: 'SQL2014'
    }
};




var updateMember = function( username, password) {
    return sql.execute( {
        procedure: "updateMember",
        params: {

            firstName: {
                type: sql.NVARCHAR,
                val: username
            },
            lastName: {
                type: sql.NVARCHAR,
                val: password
            }
        }
    } );
};

function  connecttoDb() {

  //  updateMember("elma","pass1234");
    sql.connect(config, function (err) {

        if (err) console.log("message" + err);


    });


}

module.exports.datavalue = connecttoDb();

Error logs:- Example app listening at http://:::8081 messageConnectionError: Failed to connect to 172.10.3.22:1433 - connect ECONNREF USED 172.10.3.22:1433

The thing is that the same connection string works with c# but refuses to work with Node js.

C# connection string:

<add name="ElmaReport" connectionString="Data Source=172.10.3.22\SQL2014;Initial Catalog=ElmaTest;User ID=realm;password=friend" providerName="System.Data.SqlClient" />

The solution is to enable TCP connections, because these connections are disabled by default.

1)Open Sql Server Configuration Manager .

2)Expand Sql Server Network Configuration .

3)Click Protocols for MSSQL .

4)Change TCP/IP to enabled .

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM