【发布时间】:2021-07-31 15:56:10
【问题描述】:
第一次为 Azure SQL Server 数据库创建 Node/Express api。看起来这只是延长超时限制的问题,但它也只是与数据库的连接,而不是实际的查询。这对于连接到 Azure 是正常的吗?实际上是延长超时将解决的问题还是其他问题?
index.js
var express = require('express');
var router = express.Router();
const sql = require("../dboperation")
/* GET root route */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
// Test db connection
router.get('/testconnect', function(req, res, next) {
sql.getdata();
res.render('index', { title: 'Express' });
});
module.exports = router;
dboperation.js
var config = require("./dbconfig")
const sql = require("mssql")
async function getdata(){
try {
let pool = await sql.connect(config)
console.log("SQL Server connnected...")
} catch(error) {
console.log("error: " + error)
}
}
module.exports = {
getdata: getdata,
}
dbconfig.js
const config = {
user : "username",
password : "password",
server : "server-name.database.windows.net",
database : "database-name",
options: {
trustedConnection: true,
enableArithAort: true,
encrypt: true
},
port: 49678
}
module.exports = config;
SQL Server 配置管理器:
【问题讨论】:
-
我假设 Azure 中的防火墙允许您的 IP 连接到远程实例?
-
“我该如何解决”,这取决于问题所在。这不是编程问题。
-
@Larnu - 是的,Azure 防火墙已设置为允许我的 IP。我实际上可以在 SSMS 中成功运行对 Azure 数据库的查询。
-
等等,我刚刚注意到,您提供了用户名和密码并指定
trustedConnection: true;那没有意义。您可以提供用户名和密码或使用受信任的连接。 -
我删除了
trustedConnection: true,但仍然出现错误。
标签: node.js sql-server azure azure-sql-database