无法将 Node.js 服务器连接到 Azure SQL 数据库

问题描述 投票:0回答:2

我在 Heroku 上运行一个简单的 Node.js 服务器。我已经设置了一个 Azure SQL 数据库,我只是尝试从服务器建立到它的连接。我正在使用 tedious.js 进行连接。据我所知,我正在遵循文档中的模式,但连接没有通过。这是我的代码(更改了用户名和密码)。目前,连接函数是在浏览器向“/data”页面发出 GET 请求时调用的,但该页面永远不会加载,连接也永远不会通过。有什么指点吗?

var azure = require("azure-storage");

var Connection = require("tedious").Connection;

var config = {
  Server : "my-db.database.windows",
  username : "XXXXX",
  password : "XXXXX",
  options : {
    port: 1433,
    Database : "my-db",
    connectTimeout : 3000,
  },
};


var connection = new Connection(config);

function connect(request, response) {
  connection.on("connect", function(error) {
    //If no error, then good to go
    console.log("Connected to database! Booyah.");
    executeStatement();

    response.send("Connected to database! Booyah.");
  }, function (info) {
    console.log(info);
  });
}

exports.connect = connect;
sql-server node.js azure azure-sql-database tedious
2个回答
2
投票

我赞同社区提供的答案。这是一个可以帮助您入门的快速代码示例 -

var Connection = require('tedious').Connection;
var config = {
    userName: 'yourusername',
    password: 'yourpassword',
    server: 'yourserver.database.windows.net',
    // When you connect to Azure SQL Database, you need these next options.
    options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
    // If no error, then good to proceed.
    console.log("Connected");
    executeStatement();
    //executeStatement1();

});

var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;

function executeStatement() {
    request = new Request("SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer;", function(err) {
    if (err) {
        console.log(err);} 
    });
    var result = "";
    request.on('row', function(columns) {
        columns.forEach(function(column) {
          if (column.value === null) {
            console.log('NULL');
          } else {
            result+= column.value + " ";
          }
        });
        console.log(result);
        result ="";
    });

    request.on('done', function(rowCount, more) {
    console.log(rowCount + ' rows returned');
    });
    connection.execSql(request);
}
function executeStatement1() {
    request = new Request("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES (@Name, @Number, @Cost, @Price, CURRENT_TIMESTAMP);", function(err) {
     if (err) {
        console.log(err);} 
    });
    request.addParameter('Name', TYPES.NVarChar,'SQL Server Express 2014');
    request.addParameter('Number', TYPES.NVarChar , 'SQLEXPRESS2014');
    request.addParameter('Cost', TYPES.Int, 11);
    request.addParameter('Price', TYPES.Int,11);
    request.on('row', function(columns) {
        columns.forEach(function(column) {
          if (column.value === null) {
            console.log('NULL');
          } else {
            console.log("Product id of inserted item is " + column.value);
          }
        });
    });     
    connection.execSql(request);
}

关于防火墙规则,这取决于您运行应用程序的位置。如果您在 Heroku 上运行它,则必须添加 Heroku 服务器的 IP。它是 Linux 虚拟机吗?这是一个堆栈溢出答案,您可能想检查一下。


0
投票

首先:连接字符串需要是

cultureofthefewpractice.database.windows.net
- 你在末尾缺少
.net

第二:打开 SQL 数据库服务器的防火墙以允许来自节点服务器的流量(无论流量源自哪个 IP 地址)。 SQL 数据库允许您指定 IP 范围(和多个范围)。

© www.soinside.com 2019 - 2024. All rights reserved.