Node.js + node-tds + Sql Server

时间:2012-05-31 13:27:28

标签: node.js sql-server-2012

我正在尝试关注如何使用node.js http://cretz.github.com/node-tds/

从Sql server连接和检索数据的node-tds示例

我在连接时没有错误,但是当我尝试检索数据时,我得到以下内容:

"Error: Client must be in LOGGED IN state before executing sql
at TdsClient.sqlBatch (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds-client.js:101:13)
at Statement.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:256:37)
at Statement.execute (C:\Program Files (x86)\nodejs\node_modules\tds\lib\tds.js:2:59)
at Object.<anonymous> (C:\Program Files (x86)\nodejs\edas\app.js:67:6)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)"

我已登录,所以...我无法弄清楚发生了什么。

1 个答案:

答案 0 :(得分:4)

当连接已经建立时,你应该执行你的陈述,即

var tds = require("tds");

var conn = new tds.Connection({
  host: "localhost",
  port: 1433,
  userName: "user",
  password: "secret",
  database: "MyDb"
});

conn.connect(function(error) {
  if (error != null) {
    console.error("Received error", error);
  } else {
    console.log("Now connected, can start using");
  }
});

conn.on("connect", function() {
    var statement = conn.createStatement("SELECT ProductID, ProductName FROM Products");
    statement.on("row", function(row) {
      console.log("Received row: ", row.getValue(0));
    });
    statement.execute();
});