2017-09-14 3 views
0

Tedious Getting Started Guideによると、データベースへの接続は、次のように行われます:接続が確立されたら退屈な設定(Node.js)を定義するときにデータベースを指定する方法はありますか?

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(); 
     } 
    ); 

、クエリは次のように実行することができます。

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); 
    } 

これは魔法のように動作しますただし、次のようにリクエストを変更したいと考えています。

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

明らかに、これはないしたがって、configオブジェクトで定義されたデータベースでは、SQLはテーブルを認識しません。

私はconfigオブジェクトに以下を追加しようとしている

database: 'mydb' 

私はまだ、次のエラーを取得しています:

Invalid object name 'table'.

configオブジェクトにデータベースを定義するための正しい方法は何ですか?

答えて

1

configオブジェクトは、次のようになりする必要があります:

var config = { 
    userName: 'username', 
    password: 'password', 
    server: 'server', 
    options: { 
     database: "databaseName", 
    } 
}; 
関連する問題