与node-mssql连接时出错无效的对象名称

时间:2015-11-18 22:52:58

标签: node.js sql-server-2008-r2 node-mssql

我使用以下代码连接到Ms-SQL

var node_mssql = require('node-mssql');

/* add configuration to query object */
var queryObj = new node_mssql.Query({
    host: '127.0.0.1',     // You can use 'x.x.x.x\\instance' to connect to named instance 
    port: 1433,
    username: 'myuser',
    password: 'mypwd',
    database: 'persondb'
});

/* set table name to operate */
queryObj.table('dbo.Person');

/* set update query condition */
queryObj.where({
    'FirstName': 'Mathias',
})

/* run update query and fetch response */
queryObj.select(function(results) {
    //  success callback 
    console.log(results);
}, function(err, sql) {
    //  failed callback 
    if(err)
        console.log(err);

    console.log(sql);
});

我收到错误

Invalid object name "undefined.dbo.Person."

Select  * FROM undefined.dbo.Person WHERE FirstName = 'Mathias'

看起来服务器名称未被提取。如何解决此问题以连接到Sql-Server?

1 个答案:

答案 0 :(得分:0)

您似乎只输入了错误的参数名称(database而不是db):

var queryObj = new node_mssql.Query({
    host: '127.0.0.1',     // You can use 'x.x.x.x\\instance' to connect to named instance 
    port: 1433,
    username: 'myuser',
    password: 'mypwd',
    db: 'persondb'
});