2016-06-15 115 views
0

我正在嘗試使用節點js連接到sql服務器。我已經安裝了包mssql並使用了崇高的文本編輯器。 這是我的節點js代碼。爲什麼我的節點js沒有連接到sql server?

var sql = require('mssql'); 

var dbConfig={ 
server:'CCI-LPT-21', 
userName: 'sa', 
password: 'sa123#', 
port:1433, 
options: { 
    instanceName: 'MSSQLSERVER2K12', 
    database: 'HRApp', 
    debug: { 
     packet: false, 
     payload: false, 
     token: false, 
     data: false 
      }, 
     } 
    }; 

function getEmp() 
{ 
//console.log(dbConfig); 
var conn = new sql.Connection(dbConfig); 
var req = new sql.Request(conn); 

//console.log(conn) 

conn.connect(function(err){ 
if(err){console.log(err); 
    return; 
} 
req.query("select * from Team",function(err,recordset){ 
    if(err){ 
     console.log(err); 
     return; 
    } 
    else 
    { 
     console.log(recordset); 
    } 
    conn.close(); 
    }) 
}) 

} 

getEmp(); 

當我運行此命令從PROMT,我得到這個錯誤enter image description here

當我做CONSOLE.LOG(DBCONFIG),它是一種具有這個數據

enter image description here

回答

3

你的錯誤是您必須設置user而不是userName

檢查從doc這個例子:

var config = { 
    user: '...', 
    password: '...', 
    server: 'localhost', // You can use 'localhost\\instance' to connect to named instance 
    database: '...', 

    options: { 
     encrypt: true // Use this if you're on Windows Azure 
    } 
} 

這是用戶,而不是用戶名。

+0

謝謝@Ygalbel ... :) – akash

相關問題