【问题标题】:Failed to Connect to Azure SQL using node.js (tedious module)无法使用 node.js 连接到 Azure SQL(繁琐的模块)
【发布时间】:2017-01-30 16:37:33
【问题描述】:

我想使用 node.js 连接到 Azure SQL 数据库。我在 MSDN 博客中找到了一个文档,看到我直接使用他们的源代码进行连接。我已输入正确的凭据并成功连接到数据库。

但是,当我执行查询时,它显示对象名称无效,我的表是 dbo.Users。问题是什么?

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

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

function executeStatement() {
    request = new Request("SELECT * FROM dbo.Users;", 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);
}  

【问题讨论】:

  • 我在我这边测试了你的代码,它工作正常。请您仔细检查一下您的 Azure SQL 数据库中是否有特定的表 Users。并仔细检查您使用的数据库模式是否为dbo。并尝试在您的查询语句中删除dbo。期待您的更新。
  • 我已经检查了我的数据库信息。我发现我的表名是“Table”而不是“Users”,并且schema是dbo。
  • 但是,当我使用“SELECT * FROM Table;”时或“SELECT * FROM dbo.Table;”在查询行中,它显示错误消息“关键字表附近的语法错误”

标签: sql node.js azure


【解决方案1】:

根据您的评论,作为您的原始问题,您只是将表名误认为

我已经检查了我的数据库信息。我发现我的表名是“Table”而不是“Users”,并且schema是dbo。

现在你面临问题:

“关键字 Table 附近的语法不正确”。

由于Table是MSSQL中的关键字,你可以在https://msdn.microsoft.com/en-us/library/ms189822.aspx中找到它。在 SQL 语句中,如果表名与任何关键字冲突,我们将使用[] 包含表名。

尝试使用SELECT top 1 * FROM [Table]。此外,最好重命名名为Table 的表以解决冲突。

【讨论】:

  • 如果对您有帮助,您可以标记为答案,这对与您面临相同问题的社区有好处。谢谢。
猜你喜欢
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 2016-07-16
相关资源
最近更新 更多