Skip to content Skip to sidebar Skip to footer

Unable To Connect To Microsoft Sql Server Using Node.js,mssql And Express

I am trying to learn Node.js and created a simple project to query the local database. But I get failed to look up an instance error message. I have checked that the SQL Server ser

Solution 1:

After struggling for hours on this one finally found the answer here SQL to Node connection

It seems i have to add msnodesqlv8 package and use add the driver syntax to the config.

app.get('/test', (req, res) => {

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

// config for your databaseconst config = {
    database: 'test',
    server: '(localdb)\\v11.0',
    driver: 'msnodesqlv8',
    options : {
        trustedConnection : true
    }
};
console.log('starting sql');

const pool = new sql.ConnectionPool(config);
pool.connect().then(() => {
    //simple query
    pool.request().query('select * from employees', (err, result) => {
          if(err) res.send(err)
          else{
              return res.json({
                  data : result.recordset
              })
          }
      })
      sql.close();
})    
console.log('ending sql');    

});

Solution 2:

you will need msnodesqlv8 driver, which you have to paste it in require as

var sql = require('mssql/msnodesqlv8'),

as well as you will have to include it in driver section in config object.

var config = {
    user:"*****",
    password:"*****",
    database:"*****",
    driver: 'msnodesqlv8',
    server:"*****",
    options: {
        trustedConnection : true
        }
}

Post a Comment for "Unable To Connect To Microsoft Sql Server Using Node.js,mssql And Express"