【问题标题】:Unable to connect with SQL Server using Node.js无法使用 Node.js 连接 SQL Server
【发布时间】:2016-12-10 13:27:35
【问题描述】:

我创建了一个使用 Node.js 连接 SQL Server 的应用程序。下面是代码: app.get('/SalesStatistics', function (req, res) { var Connection = require('乏味').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);
}

});

在控制台收到以下错误:

message: '请求只能在 LoggedIn 状态下进行,不能在 Connecting 状态下进行' 代码:EIINVALIDSTATE

还尝试了来自 Github 站点的示例,但我仍然无法连接到 SQL Server。如果还有其他可能,请告诉我。

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    我刚刚遇到了同样的问题,在类似的环境下运行上述相同的代码。
    事实证明,我没有将 sql server(使用 Sql Server Management Manager)配置为接受 TCP 连接到端口(1433)。完成后,一切正常。

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 2019-09-02
      • 1970-01-01
      • 2020-05-22
      • 2015-08-29
      • 2017-01-27
      • 2016-11-08
      • 2014-04-07
      • 1970-01-01
      相关资源
      最近更新 更多