简体   繁体   English

无法使用Node.js与SQL Server连接

[英]Unable to connect with SQL Server using Node.js

I have created an application using Node.js to connect with SQL Server. 我已经使用Node.js创建了一个与SQL Server连接的应用程序。 Below is the code: app.get('/SalesStatistics', function (req, res) { var Connection = require('tedious').Connection; 下面是代码: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 消息:“只能在LoggedIn状态下进行请求,而不能在Connectioning状态下进行”代码:EIINVALIDSTATE

Also tried the sample from Github site, but still I could not connect to SQL Server. 还尝试了Github网站上的示例,但仍然无法连接到SQL Server。 Please let me know if any other possibility. 请让我知道是否还有其他可能性。

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). 原来,我没有配置sql server(使用Sql Server Management Manager)以接受与端口(1433)的TCP连接。 After I done that, everything work fine. 完成之后,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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