2017-02-23 163 views
2

這是我的代碼:MSSQL的NodeJS ConnectionError:用戶登錄失敗'

var sql = require("mssql"); 

var dbConfig = { 
server: "server", 
username: "user", 
password: "password", 
database: "database" 
}; 

function getEmp() { 
var conn = new sql.Connection(dbConfig); 
console.log(conn); 
var req = new sql.Request(conn); 
conn.connect(function (err) { 
    if (err) { 
     console.log(err); 
     return; 
    } 
    req.query("SELECT * FROM Alg.User", function (err, recordset) { 
     if (err) { 
      console.log(err); 
      return; 
     } 
     else { 
      console.log(recordset); 
     } 
     conn.close(); 
    }); 
}); 
} 

getEmp(); 

這是我記錄錯誤:

{ ConnectionError: Login failed for user ''. 
at Connection.<anonymous> (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\mssql\lib\tedious.js:378:25) 
at Connection.g (events.js:291:16) 
at emitOne (events.js:96:13) 
at Connection.emit (events.js:188:7) 
at Connection.processLogin7Response (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:672:16) 
at Connection.message (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:1082:21) 
at Connection.dispatchEvent (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:519:45) 
at MessageIO.<anonymous> (c:\users\milan\documents\visual studio 2015\Projects\SampleSQLConn\SampleSQLConn\node_modules\tedious\lib\connection.js:439:23) 
at emitNone (events.js:86:13) 
at MessageIO.emit (events.js:185:7) 
name: 'ConnectionError', 
message: 'Login failed for user \'\'.', 
code: 'ELOGIN' } 

任何人都知道我在做什麼錯?它看起來像我的變量用戶名不放在連接字符串?但我不知道爲什麼...

在此先感謝!

解決方法:

的問題是,在DBCONFIG,用戶名變量應改爲用戶!而且sql查詢也是錯誤的,它應該是[Alg]。[User],因爲'User'是關鍵字!

回答

0

這爲我工作: 相反,我把它改成 var sql = require("mssql/msnodesqlv8");

我沒有硬編碼的用戶名/密碼的

var sql = require("mssql"); 

。 這裏是我的DBCONFIG是什麼樣子:

var db_config = { 
       driver: "msnodesqlv8", 
       server: "ServerName", 
       database: "databaseName", 
       options: { 
       trustedConnection: true, 
       useUTC: true 
       } 
      } 

希望這可以幫助別人

+0

我得到找不到模塊「msnodesqlv8」 – John

0

對於谷歌的觀衆......

如果上述解決方案並不爲你工作。您應該檢查計算機服務中的SQL Server瀏覽器正在運行,因爲這是將請求處理到服務器的過程。

那一個難倒我了一會兒