50198

Unable to connect to Microsoft SQL Server using Node.js,mssql and express

<h3>Question</h3>

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

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

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

<blockquote>

{ 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\ReactWithSql\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

</blockquote>
<h3>Answer1:</h3>

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

});


<h3>Answer2:</h3>

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

来源:https://stackoverflow.com/questions/55641248/unable-to-connect-to-microsoft-sql-server-using-node-js-mssql-and-express

Recommend

  • ggplotly text aesthetic causing geom_line to not display
  • Returned value cannot be converted to WebElement
  • How to send a link to Vimeo?
  • Color axis spine with multiple colors using matplotlib
  • matplotlib and transparency figure
  • Shell script to execute nohup against an inputed filename
  • JavaFX resize children relative to parent
  • Preloading webView doesn't work - trying to reduce loading time
  • How to convert days into months using datetime in Python3?
  • Auto send email based on the time and email address in database
  • Google TV VideoView playing YouTube rtsp videos
  • Defer unused CSS
  • Unsupported ciphersuite TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  • Implementation of timeout in LDAP
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • How to display content depending on dropdown menue user selection
  • cSPADE data mining in R using arulesSequences - Error while converting to “transactions” format
  • Admob in ListView not clickable
  • Set initial vuetify v-select value
  • Haskell program that can handle any arbitrary deterministic finite automaton
  • Swift manually rotate view controller
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • JavaMail connection problems [duplicate]
  • Add checkbox dynamically using angular 2
  • readmore button or show/hide for php
  • How to use array in autohotkey?
  • Getting the type of a “Type” in C# reflection
  • Generate and export point cloud from Project Tango
  • Ajax call on Multiple selection in Select box
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • Can a PHP script be scheduled to run at a specific time or after a specific amount of time has expir
  • Using redis as an LRU cache for postgres
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Capture SIGFPE from SIMD instruction
  • Using Service Component Runtime
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • XSLT Transformation to validate rules in XML document