Reputation:
I am trying to connect my nodejs/express app to SQL Server 2017 using Sequelize ORM. This is my first time of using SQL Server. I am not sure where is the problem.
Situation:-
I create a database using SQL Server Management Studio
I create an another user under security > login (for eg:- userName, passWord)
And make this user the owner of that database
Issues:
When I tried to connect to SQL Server using Management Studio (using SQL Server authentication), my connection is established every time.
But when I tried to connect same database with my nodejs app I got errors:
Error:- [SequelizeAccessDeniedError: Login faled for user '']##
Note: I already tried mssql
npm package but could not connect to SQL Server from nodejs
Here is my code for index.js file using sequelize
const express = require('express');
const app = express();
const Sequelize = require('sequelize');
const sequelize = new Sequelize("demo", "demoUser", "dPass", {
host: "localhost",
dialect: "mssql",
pool: {
max: 1,
min: 0,
idle: 5000,
acquire: 5000
},
dialectOptions: {
encrypt: true
}
});
sequelize
.authenticate()
.then(() => {
console.log("connection established");
})
.catch(err => {
if (err) {
console.log(`unable to connect database Error ${err}`);
}
});
app.listen(3000, (err) => {
if (err) throw err;
console.log("Server connect to port 3000");
})
Upvotes: 1
Views: 602
Reputation: 1219
you have to add npm package sequelize-msnodesqlv8
const Sequelize = require('sequelize');
let connectionString = {
dialect: 'mssql',
dialectModulePath: 'msnodesqlv8/lib/sequelize',
dialectOptions: {
driver: "SQL Server Native Client 11.0",
instanceName: 'MSSQLSERVER',//in my condition //check it in server configuration manager which instance is running
trustedConnection: true
},
host: 'localhost',
database: 'dbname'
}
const sequelize = new Sequelize(connectionString);
driver-go to odbc driver
then in 'system dns' tab
Add
select SQL Server Native Client 11.0 then press finish
put name as SQL Server Native Client 11.0 then select server from dropdown then next
next
next
next
finish
test data source
Upvotes: 1