[Node.js][已解決]連線mssql 失敗

文、意如

開啟 Sql Server Configuration Manager

C:\ Windows \ System32 找到 SQLServerManager15.msc

如果不想直接打開,可以執行mmc.exe把功能加進去

開始→mmc.exe

 

 

 

 

 

 

 

 

 

檔案→新增/移除嵌入式管理單元

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Sql Server Configuration Manager(組態管理員)

→SQL Server Network Configuration(組態網路)

→Protocols for (MSSQLSERVER 的通訊協定)

→TCP/IP 

我的伺服器名稱為T490\SQLY

所以這裡也要開啟

開啟SQL Server 服務 > SQL Server Browser >>啟動

都要啟動

 

程式碼:

confing/db.js

var mssql = require('mssql');
const db =
   {
       "server":'localhost\\SQLY',
 	   "user": "sa",
       "password": "y123456",
       "database": "Sample",
 
   };
mssql.connect(db, function (err) {
   if (err){
 console.log("conn-error");
 console.log(err);
}else{
 console.log("conn-success");
}
});
module.exports = db;

app.js

var db = require('./config/db');

//db
app.use(function (req, res, next) {
 req.db = db;
 next();
});

pm2 log

成功連線但是換另一種錯誤

GMT tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. at node_modules\mssql\lib\tedious\connection-pool.js:61:23

先在db.js

補上即可

"options": {
  "enableArithAbort": true,
  "encrypt": true
 }

完整:

var mssql = require('mssql');
const db =
   {
       "server":"localhost\\SQLY",  #這裡要注意
       "user": "sa",
       "password": "y123456",
       "database": "Sample",
       "options": {
              "enableArithAbort": true,
              "encrypt": true
 }  
   };
mssql.connect(db, function (err) {
   if (err){
 console.log("conn-error");
 console.log(err);
}else{
 console.log("conn-success");
}
});
module.exports = db;

暫時關掉,沒紅字了,解決!

Yiru@Studio - 關於我 - 意如