SQL Server is not connecting with node app

时间:2019-04-17 02:04:58

标签: node.js express sequelize.js sql-server-2017-express

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

1 个答案:

答案 0 :(得分:0)

  

您必须添加npm软件包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-转到odbc驱动程序

     

然后在“系统dns”标签中

     

添加

     

选择“ SQL Server Native Client 11.0”,然后按“完成”

     

将名称输入为SQL Server Native Client 11.0,然后从中选择服务器    下拉菜单,然后下一步

     

下一个

     

下一个

     

下一个

     

完成

     

测试数据源