无法使用繁琐的 Azure 数据库

Unable to use tedious with Azure database

我有一个节点应用程序,它连接到 SQL 服务器。
另外,我正在使用 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,您需要提供一个额外的加密连接选项。

请为 config 尝试以下操作:

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 中托管的数据库。