简体   繁体   English

node.js 未使用 SQL Server 身份验证连接到 SQL Server 数据库

[英]node.js is not connecting to SQL Server database using SQL Server authentication

I'm using node.js and the mssql package to connect to a SQL Server database using SQL Server authentication.我正在使用 node.js 和mssql包通过 SQL Server 身份验证连接到 SQL Server 数据库。 When I try connecting using SQL Server Management Studio with the same credentials, it is working fine.当我尝试使用具有相同凭据的 SQL Server Management Studio 进行连接时,它工作正常。 However, with node.js, I cannot login and get an error code ELOGIN with connection error.但是,对于 node.js,我无法登录并收到带有连接错误的错误代码 ELOGIN。

I've tried many examples shown in google and I'm facing the same issue.我已经尝试了谷歌中显示的许多示例,但我面临着同样的问题。 Let me know what I'm missing.让我知道我错过了什么。 Here is the code snippet of mine.这是我的代码片段。

Code starts here代码从这里开始

var sql = require('mssql');
var config = {
  server: 'scaXXXXXXXXXXXX',
  database: 'scaXXXXXXXXXX',
  user: 'svcXXXXXXX',
  password: 'Password',
  port: 1433
};

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

listProducts();

This is the error while running this code:这是运行此代码时的错误:

ConnectionError: Login failed for user 'svcXXXXXXX'. ConnectionError:用户“svcXXXXXXX”登录失败。

at Connection.tedious.once.err (C:\aws\node_modules\mssql\lib\tedious.js:244:17)在 Connection.tedious.once.err (C:\aws\node_modules\mssql\lib\tedious.js:244:17)
at Object.onceWrapper (events.js:277:13)在 Object.onceWrapper (events.js:277:13)
at Connection.emit (events.js:189:13)在 Connection.emit (events.js:189:13)
at Connection.processLogin7Response (C:\aws\node_modules\tedious\lib\connection.js:1397:14)在 Connection.processLogin7Response (C:\aws\node_modules\tedious\lib\connection.js:1397:14)
at Connection.message (C:\aws\node_modules\tedious\lib\connection.js:1932:14)在 Connection.message (C:\aws\node_modules\tedious\lib\connection.js:1932:14)
at Connection.dispatchEvent (C:\aws\node_modules\tedious\lib\connection.js:1084:36)在 Connection.dispatchEvent (C:\aws\node_modules\tedious\lib\connection.js:1084:36)
at MessageIO.messageIo.on (C:\aws\node_modules\tedious\lib\connection.js:984:14)在 MessageIO.messageIo.on (C:\aws\node_modules\tedious\lib\connection.js:984:14)
at MessageIO.emit (events.js:189:13)在 MessageIO.emit (events.js:189:13)
at Message.message.on (C:\aws\node_modules\tedious\lib\message-io.js:32:14)在 Message.message.on (C:\aws\node_modules\tedious\lib\message-io.js:32:14)
at Message.emit (events.js:194:15)在 Message.emit (events.js:194:15)

code: 'ELOGIN',代码:'ELOGIN',
originalError: { ConnectionError: Login failed for user 'svcXXXXXXX'. originalError: { ConnectionError: 用户 'svcXXXXXXX' 登录失败。
at ConnectionError (C:\aws\node_modules\tedious\lib\errors.js:13:12)在 ConnectionError (C:\aws\node_modules\tedious\lib\errors.js:13:12)
at Parser.tokenStreamParser.on.token (C:\aws\node_modules\tedious\lib\connection.js:735:29)在 Parser.tokenStreamParser.on.token (C:\aws\node_modules\tedious\lib\connection.js:735:29)
at Parser.emit (events.js:189:13)在 Parser.emit(events.js:189:13)
at Parser.parser.on.token (C:\aws\node_modules\tedious\lib\token\token-stream-parser.js:27:14)在 Parser.parser.on.token (C:\aws\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
at Parser.emit (events.js:189:13)在 Parser.emit(events.js:189:13)
at addChunk (C:\aws\node_modules\readable-stream\lib_stream_readable.js:297:12)在 addChunk (C:\aws\node_modules\readable-stream\lib_stream_readable.js:297:12)
at readableAddChunk (C:\aws\node_modules\readable-stream\lib_stream_readable.js:279:11)在 readableAddChunk (C:\aws\node_modules\readable-stream\lib_stream_readable.js:279:11)
at Parser.Readable.push (C:\aws\node_modules\readable-stream\lib_stream_readable.js:240:10)在 Parser.Readable.push (C:\aws\node_modules\readable-stream\lib_stream_readable.js:240:10)
at Parser.Transform.push (C:\aws\node_modules\readable-stream\lib_stream_transform.js:139:32)在 Parser.Transform.push (C:\aws\node_modules\readable-stream\lib_stream_transform.js:139:32)
at doneParsing (C:\aws\node_modules\tedious\lib\token\stream-parser.js:80:14)在 doneParsing (C:\aws\node_modules\tedious\lib\token\stream-parser.js:80:14)

message: 'Login failed for user \'svcXXXXXXX\'.',消息:“用户 \'svcXXXXXXX\' 登录失败。”,
code: 'ELOGIN' }, name: 'ConnectionError' }代码:'ELOGIN'},名称:'ConnectionError'}

I expect one record from database should extract and display.我希望数据库中的一条记录应该提取并显示。

it looks your login information is not correct.看起来您的登录信息不正确。 did you write proper user name and password?你写了正确的用户名和密码吗?

if your login info is correct, then check out login info has authority to be connected from the external environment如果您的登录信息正确,则检查登录信息是否有权从外部环境进行连接

Try this out.试试这个。 It worked for me.它对我有用。 If you are not doing with a localhost Database you need to be in that network.如果您不使用本地主机数据库,则需要在该网络中。 Make sure you can ping the database server.确保您可以 ping 通数据库服务器。

var sql = require("mssql");
var moment = require("moment");
let port = process.env.PORT;
if (port == null || port == "") {
    port = 8000;
}

var config = {
    user: "xxxx",
    password: "xxxxx",
    server: "xxxxxx",
    database: "xxxx"
};

const dbconn = sql.connect(config, err => {
    if (!err) {
        console.log("Connected to the database");
    } else {
        console.log("Problem in connecting to database");
        console.log(err);
        console.log("testing ");
    }
});

app.get("/getSummaryDetails", (req, res) => {
    dbconn.query("exec QCGrid", (err, rows, fields) => {
        if (!err) {
            res.send(rows.recordsets[0]);
        }
    });
});

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

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