且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 错误 "[ConnectionError: Login failed for user '****'.] "使用 MSSQL 将 SQL Server 与 Nodejs 连接时

更新时间:2022-12-09 20:54:29

我看到这里没有人回答这个问题,

I see that nobody answered this question here,

下面是使用'mssql'的工作代码,

Below is the working code using 'mssql',

var sql = require('mssql');
var config = {
    server: 'localhost',
    database: 'sampleDB',
    user: 'YourUser',
    password: 'YourPassword',
    port: '1433'
};

function listProducts() {
    var conn = new sql.ConnectionPool(config);
    conn.connect().then(function () {
        var request = new sql.Request(conn);
        request.query("select * from products").then(function (recordSet) {
            console.log(recordSet);
            conn.close();
        }).catch(function (err) {
            console.log(err);
            conn.close();
        });
    }).catch(function (err) {
        console.log(err);
    });
}

listProducts();

在您的解决方案中,尝试将端口号放在引号中并使用

In your solution, try to put port number in quotes and use

var conn=new sql.ConnectionPool(config);

代替,

var conn=new sql.Connection(config);

还有另一种使用 'tedious' 连接到 sql server 的解决方案,

There is one other solution for making connection to sql server using 'tedious',

var Connection = require('tedious').Connection;

var config = {
            server: "localhost", 
            userName: "YourUser",
            password: "YourPassword",
            database: "sampleDB"
     };

var connection = new Connection (config);

connection.on('connect', function(err){
    console.log(err);
    if(err!=null){
         console.log("not connected");
    }
    else{  
          console.log("Connected")
          connection.close();
    };
});

我希望这能帮助遇到同样问题的人.

I hope this will help someone having the same issue.