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

前端 未结 12 1354
忘掉有多难
忘掉有多难 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:41

    I have been struggling too for some time about how to use mssql + Windows Auth, here is how i got it to work on my project.

    As pointed out in the mssql documentation, you need msnodesqlv8 installed too.

    npm install msnodesqlv8
    

    Now, following on Aaron Ballard's answer, you use it like this:

    const sql = require('mssql/msnodesqlv8')
    
    const pool = new sql.ConnectionPool({
      database: 'database',
      server: 'server',
      driver: 'msnodesqlv8',
      options: {
        trustedConnection: true
      }
    })
    
    pool.connect().then(() => {
      //simple query
      pool.request().query('select 1 as number', (err, result) => {
            console.dir(result)
        })
    })
    

    As a note, i tried to add this as a comment on Aaron's answer, as mine is just a complement/update to his, but i don't have enough reputation to do so.

提交回复
热议问题