Monday 12 June 2017

MSSQL NODEJS Undefined Connection error

I am trying to connect named instance of MS SQL Server 2008 using MSSQL nodejs.

var dbconfig={
        user:"sa",
        password:"password",
        server:"127.0.0.1",
        database:"products",
        options:{
            instanceName:"SQLEXPRESS"
        }
    };


//function to connect and execute query
var executeQuery = function(res,query){
    sql.connect(dbconfig,function(err){
        if(err)
        {
            console.log("connection error", err);
            res.send(err);
        }
        else
        {
            console.log("connected!");
            var request = new sql.request();
            request.query(query,function(err,res){
                if(err)
                {
                    console.log("request error",err);
                    res.send(err);
                }
                else
                {
                    res.send(res);  
                }
            });
        }
    });
    sql.close();
};


//GET API
app.get("/api/products",function(req,res){
    var query ="select * from productcategory";
    console.log("Function call");
    executeQuery(res,query);
});

and I got the below error:

{"code":"ESOCKET","originalError":{"message":"Failed to connect to 127.0.0.1:undefined - connect ECONNREFUSED 127.0.0.1:1433","code":"ESOCKET"},"name":"ConnectionError"}

I have enabled TCP\IP in Sql Server configuration Manager. Also SQL Server Browser service is running.

Tried replacing the server name with 'localhost' and 'MyPCname' but still received the same error.

Please help me with the issue.



via Vignesh

No comments:

Post a Comment