2017-10-14 80 views
0

當我嘗試從Node Js應用程序連接到SQL Server時出現以下問題。來自節點js應用程序的SQL Server連接問題

使用的SQL Server名稱DESKTOP-FCM7L54\SRINIVAS是正確的。

錯誤的詳細信息:

{ ConnectionError: Failed to connect to DESKTOP-FCM7L54SRINIVAS:1433 - getaddrinfo ENOTFOUND DESKTOP-FCM7L54SRINIVAS

at Connection.base.ConnectionPool._poolCreate.base.Promise.tedious.once.err (C:\Users\Owner\documents\visual studio 2015\Projects\NodejsConsoleApp3\SqlNodejs\node_modules\mssql\lib\tedious.js:216:17)
at Connection.g (events.js:292:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (C:\Users\Owner\documents\visual studio 2015\Projects\NodejsConsoleApp3\SqlNodejs\node_modules\tedious\lib\connection.js:699:14)
at C:\Users\Owner\documents\visual studio 2015\Projects\NodejsConsoleApp3\SqlNodejs\node_modules\tedious\lib\connection.js:590:25
at C:\Users\Owner\documents\visual studio 2015\Projects\NodejsConsoleApp3\SqlNodejs\node_modules\tedious\lib\connector.js:68:18
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:62:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:89:17)
Code: 'ESOCKET',

var express = require('express'); 
var app = express(); 
var sql = require("mssql"); 

// config for your database 
var config = { 
    server: 'DESKTOP-FCM7L54\SRINIVAS', 
    database: 'Test', 
    user: 'sa', 
    password: 'test', 
    port: 1433 
}; 

app.get('/', function (req, res) { 
    //connect to your database 
    sql.connect(config, function (err) { 
     if (err) console.log(err); 
     // create Request object 
     var request = new sql.Request(); 
     // query to the database and get the records 
     request.query('select * from Customer', function (err, recordset) { 
      if (err) console.log(err) 
      // send records as a response 
      res.send(recordset); 
     }); 
    }); 
    sql.close(); 
}); 

var server = app.listen(5000, function() { 
    console.log('Server is running..'); 
}); 

回答

0

你所得到的錯誤代碼是ENOTFOUND,這意味着找不到連接。驗證您的主機和端口,並確認服務確實在此端口上運行。
還有一個人認爲你可以看到你的錯誤 - 它說getaddrinfo ENOTFOUND DESKTOP-FCM7L54SRINIVAS - 注意到你的名字中缺少的反斜槓,可能是逃避它。也許嘗試連接到DESKTOP-FCM7L54\\SRINIVAS

我在代碼中看到的另一件事,與此問題無關 - 您沒有正確處理錯誤 - 將它們打印到控制檯,但不阻止代碼的流動會造成一些不良行爲。