SQL Server Error “[ConnectionError: Login failed f

2019-06-05 03:26发布

问题:

I am getting below error

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

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();

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

回答1:

I see that nobody answered this question here,

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);

instead of,

var conn=new sql.Connection(config);

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.