简体   繁体   中英

SQL Server is not connecting with node app

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:-

  1. I create a database using SQL Server Management Studio

  2. I create an another user under security > login (for eg:- userName, passWord)

  3. And make this user the owner of that database

Issues:

  1. When I tried to connect to SQL Server using Management Studio (using SQL Server authentication), my connection is established every time.

  2. 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");
})
   

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

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.

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