user6677945
user6677945

Reputation: 1

Unable to connect with SQL Server using Node.js

I have created an application using Node.js to connect with SQL Server. Below is the code: app.get('/SalesStatistics', function (req, res) { var Connection = require('tedious').Connection;

// config for your database
var config = {
    user: "****",
    password: "*****",
    server: "abc",
    database: "xyz"
};

var connection = new Connection(config);
connection.on('connect', function (err) {       
    // If no error, then good to proceed.  
    console.log("Connected");
    executeStatement();  
});
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;  

function executeStatement() {
    request = new Request("select * from employee;", function (err) {
        if (err) {
            console.log(err);
        }
    });
    var result = "";
    request.on('row', function (columns) {
        columns.forEach(function (column) {
            if (column.value === null) {
                console.log('NULL');
            } else {
                result += column.value + " ";
            }
        });
        console.log(result);
        result = "";
    });
    request.on('done', function (rowCount, more) {
        console.log(rowCount + ' rows returned');
    });
    connection.execSql(request);
}

});

Received the below error in console:

message: 'Requests can only be made in the LoggedIn state, not the Connecting state' code: EIINVALIDSTATE

Also tried the sample from Github site, but still I could not connect to SQL Server. Please let me know if any other possibility.

Upvotes: 0

Views: 152

Answers (1)

user7026025
user7026025

Reputation: 1

I just encountered the same problem awhile ago running the same code above with similar environment.
It turn out that I did not configure the sql server (using Sql Server Management Manager) to accept TCP connection to port (1433). After I done that, everything work fine.

Upvotes: 0

Related Questions