繁体   English   中英

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

[英]Can't connect Node.js server to Azure SQL Database

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

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;

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

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吗? 这是您可能需要检查的堆栈溢出答案

第一:连接字符串必须是cultureofthefewpractice.database.windows.net最后缺少.net

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

暂无
暂无

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

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