繁体   English   中英

与 rest-API 和 SQL 服务器或 Azure 的连接

[英]Connection with rest-API and SQL server or Azure

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

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

ia create 的 rest-API 在 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.

连接到 Azure SQL 数据库:

以下是使用 REST-API 连接到 Azure SQL 数据库的示例代码: 在此处输入图片说明

注意: encrypt必须为true

您可以从 Portal 获取服务器名称: 在此处输入图片说明

并且您需要将客户端 IP 地址添加到Azure SQL Server 防火墙设置 在此处输入图片说明

然后就可以连接到 Azure SQL 数据库了。 您可以参考:在 Azure 中创建 Node.js REST API

连接到 SQL Server:

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

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

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

希望这可以帮助。

欢迎使用 StackOverflow。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM