【问题标题】:Connection with rest-API and SQL server or Azure与 rest-API 和 SQL 服务器或 Azure 的连接
【发布时间】:2019-12-02 10:08:57
【问题描述】:

我在连接到位于 Azure 中的数据库时遇到问题,我试图与我创建的 rest-API 连接到我在 Azure 中拥有的数据库,我直接从 SQL Server 管理这个数据库,我无法与此建立联系。

我尝试连接 SQL Server 中的另一个测试数据库。

rest-API ia create 在 NodeJS 中

var sql = require('mssql');
var dbconfig = {
    server:"Fernando\EQUIPO",
    user: "<user>",
    password: "<password>",
    database: "<dbname>",
    port: 1433,
    option: {
        encrypt: false
    }
};

function getList() {
    var record;
    var conn = new sql.ConnectionPool(dbconfig);
    conn.connect(function(err){
        if(err) throw err;
        var req = new sql.Request(conn);
        req.query("select * from cliente", function(err, recordset) {
            if(err) throw err;
            else {
                console.log(recordset);
                record = recordset;
            }
            conn.close();
        });
    });
    return record;
}

const { Router } = require('express');
const router = Router();

const _ = require('underscore');

const movies = require('../sample.json');

router.get('/', (req, res) => {
    res.send(getList());
});

当我对本地主机 http://localhost:3000/api/movies 进行“获取”时,控制台中会出现以下消息:

GET /api/movies 200 126.188 ms - -
(node:11868) UnhandledPromiseRejectionWarning: ConnectionError: Failed to connect to FernandoEQUIPO:1433 - getaddrinfo ENOTFOUND FernandoEQUIPO
    at Connection.tedious.once.err (C:\Users\luisn\Desktop\rest-API\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at Connection.socketError (C:\Users\luisn\Desktop\rest-API\node_modules\tedious\lib\connection.js:1258:12)
    at _connector.Connector.execute (C:\Users\luisn\Desktop\rest-API\node_modules\tedious\lib\connection.js:1084:21)
    at GetAddrInfoReqWrap._dns.default.lookup [as callback] (C:\Users\luisn\Desktop\rest-API\node_modules\tedious\lib\connector.js:152:16)        
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:68:17)
(node:11868) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11868) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

    标签: javascript node.js sql-server azure azure-sql-database


    【解决方案1】:

    连接到 Azure SQL 数据库:

    以下是使用 REST-API 连接到 Azure SQL 数据库的示例代码:

    注意:encrypt 必须是 true

    您可以从 Portal 上获取服务器名称:

    并且您需要将客户端IP地址添加到Azure SQL Server firewall settings

    那么您现在可以连接到 Azure SQL 数据库了。可以参考:Creating a Node.js REST API in Azure

    连接到 SQL Server:

    您的代码几乎是正确的,需要进行一些更改:

    var sql = require('mssql');
    var dbconfig = {
        server:"localhost",
        user: "<user>",
        password: "<password>",
        database: "<dbname>",
        port: 1433,
        option: {
            encrypt: false
        }
    };
    

    就像 Abhishek Ranjan 说的,你应该输入你的服务器 ip。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      欢迎来到 StackOverflow。

      ConnectionError: Failed to connect to FernandoEQUIPO:1433 - getaddrinfo ENOTFOUND FernandoEQUIPO
      

      您的错误表明它无法连接,因为它在给定地址找不到服务器。请确保有服务器正在运行并使用某些第三方应用程序验证连接。
      您确定 FernandoEQUIPO被解析为正确的主机名?

      【讨论】:

      • 我的服务器名称是 Fernando\EQUIPO
      • 我想你需要在这里输入你的服务器 ip
      • 你说得对,这解决了我的问题,我也改变了服务器的端口,谢谢
      • 在天蓝色,我必须放什么 ip?
      • @Fernando 如果这对您有帮助,您应该接受它作为答案,这就是当其他人来寻找相同问题时,他知道该怎么做。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多