我有一个 Node 应用程序,它确实有到 SQL Server 的连接。
另外,我正在使用 Azure 的数据库即服务。
代码片段:
import { Connection } from 'tedious';
import { Request } from 'tedious';
var config = {
userName: 'dbuser',
password: 'dbpassword',
server: 'mydatabase.database.windows.net',
options: {
instanceName: 'SQLEXPRESS', // Removed this line while deploying it on server as it has no instance.
database: 'dbname'
}
};
connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log('error : '+err);
} else {
console.log("Connected to Database");
}
});
如果已在本地完成连接,则已成功连接。
控制台输出 => 连接到数据库。
使用控制台日志进行深入研究:
-> Connection object is being created, but, the event ".on" is not being able to establish.
-> The connection gets established when deployed locally, while, when deployed on server, it doesn't works.
最佳答案
基于文档here
,您需要提供一个额外的加密连接选项。
请尝试以下配置
:
var config = {
userName: 'dbuser',
password: 'dbpassword',
server: 'mydatabase.database.windows.net',
options: {
database: 'dbname',
encrypt: true //Need to add this for connecting to Azure DB
}
};
使用此配置,我能够连接到 Azure 中托管的数据库。
关于node.js - 无法使用Azure数据库进行繁琐的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505548/