关于sql server:定义Tedious配置(Node.js)时是否可以指定数据库?

Is there a way to specify the database when defining Tedious configuration (Node.js)?

根据《 Tedious入门指南》,连接数据库的操作如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var Connection = require('tedious').Connection;

      var config = {
        userName: 'test',
        password: 'test',
        server: '192.168.1.210',

        // If you're on Windows Azure, you will need this:
        options: {encrypt: true}
      };

      var connection = new Connection(config);

      connection.on('connect', function(err) {
        // If no error, then good to go...
          executeStatement();
        }
      );

建立连接后,可以执行以下查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var Request = require('tedious').Request;

  function executeStatement() {
    request = new Request("select 42, 'hello world'", function(err, rowCount) {
      if (err) {
        console.log(err);
      } else {
        console.log(rowCount + ' rows');
      }
    });

    request.on('row', function(columns) {
      columns.forEach(function(column) {
        console.log(column.value);
      });
    });

    connection.execSql(request);
  }

这就像一种魅力,但是,我想将请求更改为以下内容:

1
 request = new Request("select * from table", function(err, rowCount) {

显然,这将不起作用,因为在config对象中没有定义数据库,因此,SQL无法识别该表。

我尝试将以下内容添加到配置对象:

1
database: 'mydb'

我仍然收到以下错误:

Invalid object name 'table'.

在config对象中定义数据库的正确方法是什么?


配置对象应如下所示:

1
2
3
4
5
6
7
8
var config = {
  userName: 'username',
  password: 'password',
  server: 'server',
  options: {
      database:"databaseName",
  }
};