This is my code:
var sql = require("mssql");
var dbConfig = {
server: "server",
username: "user",
password: "password",
database: "database"
};
function getEmp() {
var conn = new sql.Connection(dbConfig);
console.log(conn);
var req = new sql.Request(conn);
conn.connect(function (err) {
if (err) {
console.log(err);
return;
}
req.query("SELECT * FROM Alg.User", function (err, recordset) {
if (err) {
console.log(err);
return;
}
else {
console.log(recordset);
}
conn.close();
});
});
}
getEmp();
And this is the error I'm logging:
{ ConnectionError: Login failed for user ''.
at Connection.<anonymous> (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\mssql\lib\tedious.js:378:25)
at Connection.g (events.js:291:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.processLogin7Response (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:672:16)
at Connection.message (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:1082:21)
at Connection.dispatchEvent (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:519:45)
at MessageIO.<anonymous> (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:439:23)
at emitNone (events.js:86:13)
at MessageIO.emit (events.js:185:7)
name: 'ConnectionError',
message: 'Login failed for user \'\'.',
code: 'ELOGIN' }
Anyone knows what I'm doing wrong? It looks like my variable username isn't put in the connectionstring? But I have no idea why...
Thanks in advance!
Solution:
The problem was that in dbConfig, the username variable should be changed to user! And the sql query was wrong also, it should have been [Alg].[User], because 'User' is a keyword!
5条答案
按热度按时间lfapxunr1#
Using tedious / msnodesqlv8 driver for Windows worked for me:
Instead of:
I changed it to:
I did not hard code user name/password.
Here's what my dbconfig looks like:
dpiehjr42#
Please make "username" to "user" in your configuration. If you try to console.log(conn), you will see that configuration is using "user" instead of userName.
I have answered this question, please refer to SQL Server Error "[ConnectionError: Login failed for user '****'.] "while connecting SQL Server with Nodejs using MSSQL
p3rjfoxz3#
For google viewers...
If the above solution doesn't work for you. You should check the SQL Server Browser in Computer services is running as this is what processes the requests to the server.
That one stumped me for a while
pkwftd7m4#
Change 'username' of dbConfig to 'user'
xeufq47z5#
For me, connecting to an Azure SQL database, this was fixed by adding this to my mssql.config. I did not need to specify a particular driver, as @shahreen did