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

查看:98
本文介绍了SQL Server 错误 "[ConnectionError: Login failed for user '****'.] "使用 MSSQL 将 SQL Server 与 Nodejs 连接时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误

      [ConnectionError: Login failed for user '****'.]
             name: 'ConnectionError',
             message: 'Login failed for user \'****\'.',
             code: 'ELOGIN' } "

下面是我使用 mssql 包连接 sql server 和 nodejs 的代码.

Below is the code that i used to connect sql server with nodejs using mssql package.

var sql = require('mssql');

var config = {
        server: "localhost\\",// You can use 'localhost\\instance' to connect to named instance 
        database: "***",
        user: "***",
        password: "",
        port: 1433
    };

function getdata() {
    var conn = new sql.Connection(config);
    var req = new sql.Request(conn);

    conn.connect(function (err) {
        If (err) {
            console.log(err);
            return;
        }
        req.query("select * From" ,function (err,recordset)  {
        If (err) {
            console.log(err);
            return;
        }
        else {
            console.log(recordset);

        }
        conn.close();


        enter code here
        });

    });

    }

    getdata();

以下是我将控制台添加到conn"时显示的消息,如果我错过了什么,请告诉我.

Below is the message displayed when i add console to "conn" ,Kindly let me know if i have missed anything.

config:
      { server: '',
        database: '',
        user: '',
        password: '',
        port: 1433,
        driver: 'tedious',
        options: {},
        stream: false,
        parseJSON: false },
     driver:
      { Connection:
         { [Function: TediousConnection]
           EventEmitter: [Object],
           usingDomains: false,
           defaultMaxListeners: 10,
           init: [Function],
           listenerCount: [Function],
           __super__: [Object] },
        Transaction:
         { [Function: TediousTransaction]
           EventEmitter: [Object],
           usingDomains: false,
           defaultMaxListeners: 10,
           init: [Function],
           listenerCount: [Function],
           __super__: [Object] },
        Request:
         { [Function: TediousRequest]
           EventEmitter: [Object],
           usingDomains: false,
           defaultMaxListeners: 10,
           init: [Function],
           listenerCount: [Function],
           __super__: [Object] },
        fix: [Function] } }
    started

推荐答案

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

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.

这篇关于SQL Server 错误 "[ConnectionError: Login failed for user '****'.] "使用 MSSQL 将 SQL Server 与 Nodejs 连接时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆