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

时间:2016-05-10 16:48:03

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

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

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

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

var config = {
  Server : "cultureofthefewpractice.database.windows",
  username : "XXXXX",
  password : "XXXXX",
  options : {
    port: 1433,
    Database : "cultureofthefewpracticedatabase",
    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;

2 个答案:

答案 0 :(得分: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 VM吗?这是您可能想要查看的堆栈溢出answer

答案 1 :(得分:0)

首先:连接字符串必须为cultureofthefewpractice.database.windows.net - 您最后会遗漏.net

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