简体   繁体   English

从 Node.js 连接到 Azure SQL 数据库时没有任何反应

[英]Nothing happens when connecting to Azure SQL Database from Node.js

Following the Microsoft provided instructions, I used the below code.按照 Microsoft 提供的说明,我使用了以下代码。 https://docs.microsoft.com/en-us/azure/azure-sql/database/connect-query-nodejs?tabs=windows https://docs.microsoft.com/en-us/azure/azure-sql/database/connect-query-nodejs?tabs=windows

const { Connection, Request } = require("tedious");

// Create connection to database
const config = {
  authentication: {
    options: {
      userName: "username", // update me
      password: "password" // update me
    },
    type: "default"
  },
  server: "your_server.database.windows.net", // update me
  options: {
    database: "your_database", //update me
    encrypt: true
  }
};

const connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
  if (err) {
    console.error(err.message);
  } else {
    queryDatabase();
  }
});

When I run the app, nothing happens and nothing is logged.当我运行该应用程序时,没有任何反应,也没有任何记录。 How do I connect to an Azure SQL Database using Node.js?如何使用 Node.js 连接到 Azure SQL 数据库?

I found the answer as a comment on another Azure Database issue, provided by @akkonrad.我找到了答案作为对@akkonrad 提供的另一个 Azure 数据库问题的评论。 Simply adding a connect statement prior to the provided code from Microsoft, it is now working.只需在 Microsoft 提供的代码之前添加一个连接语句,它现在就可以工作了。

connection.connect(); //<---- Add This Line ----
connection.on("connect", err => {
    if (err) {
      console.error(err.message);
    } else {
      queryDatabase();
    }
});

Hope this prevents someone from digging for hours for the solution.希望这可以防止有人为解决方案挖掘数小时。

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

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