简体   繁体   中英

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 services running in services.msc

服务.msc图片

I have verified TCP/IP is enabled

配置管理图片

I have tried with the username and password and without it as well. I connect to localdb in SQL Server Management Studio as (localdb)\\v11.0 and below is the screenshot of the properties

SQL Server 连接属性的图像

What am I doing incorrectly? What should be actual username and password? What should be the servername?

const sql = require('mssql');

// config for your database
const config = {
    user: 'mywindows username',
    password: 'my windows password',
    server: '(localdb)\\v11.0', 
    database: 'test',
    options: {
        encrypt: true
      } 
};
console.log('starting sql');

var connection = new sql.connect(config, function(err) {
    console.log(err);
    var request = new sql.Request(connection); 
    request.query('select * from employees', function(err, recordset) {
       if(err)      // ... error checks 
            console.log('Database connection error');

    console.dir("User Data: "+recordset);
    });
});
sql.close();
console.log('ending sql');    
});

app.listen(3002, () => {
    console.log('Listening on port 3002');})

Below is the error message

{ ConnectionError: Failed to lookup instance on (localdb) - getaddrinfo ENOTFOUND (localdb) at Connection.tedious.once.err (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\ReactWithSql\\node_modules\\mssql\\lib\\tedious.js:244:17) at Object.onceWrapper (events.js:285:13) at Connection.emit (events.js:197:13) at InstanceLookup.instanceLookup (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\ReactWithSql\\node_modules\\tedious\\lib\\connection.js:945:16) at sender.execute (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\ReactWithSql\\node_modules\\tedious\\lib\\instance-lookup.js:66:13) at GetAddrInfoReqWrap.invokeLookupAll [as callback] (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\ReactWithSql\\node_modules\\tedious\\lib\\sender.js:43:16) at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:70:17) code: 'EINSTLOOKUP', originalError: { ConnectionError: Failed to lookup instance on (localdb) - getaddrinfo ENOTFOUND (localdb) at ConnectionError (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\Reac tWithSql\\node_modules\\tedious\\lib\\errors.js:13:12) at InstanceLookup.instanceLookup (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\ReactWithSql\\node_modules\\tedious\\lib\\connection.js:945:32) at sender.execute (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\ReactWithSql\\node_modules\\tedious\\lib\\instance-lookup.js:66:13) at GetAddrInfoReqWrap.invokeLookupAll [as callback] (C:\\Users\\vndbsubramaniam\\Desktop\\React projects\\ReactWithSql\\node_modules\\tedious\\lib\\sender.js:43:16) at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:70:17) message: 'Failed to lookup instance on (localdb) - getaddrinfo ENOTFOUND (localdb)', code: 'EINSTLOOKUP' }, name: 'ConnectionError' } Database connection error

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 database
const 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');    

});

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

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