How to connect to SQL Server with windows authentication from Node.JS using mssql module

前端 未结 12 1356
忘掉有多难
忘掉有多难 2020-11-27 13:57

Hi I\'m unable to connect to SQL server that is using windows authentication in node js. I\'m using the mssql module. The error message is :

[ConnectionError         


        
12条回答
  •  北海茫月
    2020-11-27 14:39

    This version doesn't need a username or password.

    To use windows authentication I installed mssql and msnodesqlv8.

    Then in my app.js file:

    const mssql = require('mssql/msnodesqlv8'); 
    

    Note it is mssql not sql if you're using this example.

    var config = {
      database:'YOUR DATABASE NAME',  
      server: 'localhost\\SQLEXPRESS',
      driver: 'msnodesqlv8',
      options: {
        trustedConnection: true,
        enableArithAbort: true
      }
    };
    

    You need to change the database name in config. Other than that it should work. My example:

    app.get('/', function (req, res) {
    
            mssql.connect(config, function (err) {
        
                if (err) console.log(err);
                var request = new mssql.Request();
                request.query('select * from dbo.visit', function (err,  result) {
                    if(err) console.log(err);
                    console.log(result);
                });
        
            });
        
     });
    

提交回复
热议问题