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
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.
It may be too late to answer, but this recently happened with me and it drove me crazy!!! I was trying to connect my db to express and I was working with windows authentication mode. For two long days I kept googling and refreshing until I got this article: https://www.liquidweb.com/kb/troubleshooting-microsoft-sql-server-error-18456-login-failed-user/
So in a nutshell;
First I installed the msnodesqlv8
driver for windows authentication, then in the my server on ssms, I right clicked on my server then in properties and then security, I enabled sql server and windows authentication mode, then in the object explorer, clicked on the plus next to the server, then security, then logins. There I saw sa
with a cross next to it. In it's properties, I changed my password to something easier to remember (optional), then in the status, set the login to enable. PHEW!
Now my config code: <br / >
const config = {
user: 'sa',
password: '123',
driver: "msnodesqlv8",
server: 'UZAIR-S_PC\\SQLEXPRESS',
database: 'LearningExpressJS',
options: {
trustedconnection: true,
enableArithAbort : true,
trustServerCertificate: true,
instancename : 'SQLEXPRESS'
},
port: 58018
}
This works finally !!!!!!!!!!!!!!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.