无法使用Node.js,mssql和express

时间:2019-04-11 21:12:48

标签: node.js sql-server

我正在尝试学习Node.js,并创建了一个简单的项目来查询本地数据库。但是我无法查找实例错误消息。

我已经检查了services.msc中运行的SQL Server服务

services.msc picture

我已确认启用了TCP / IP

configurations manage pic

我尝试使用用户名和密码,也没有。我以(localdb)\v11.0的身份连接到SQL Server Management Studio中的localdb,下面是属性的屏幕截图

image of SQL Server connection properties

我做错了什么?实际的用户名和密码应该是什么?服务器名称应该是什么?

const sql = require('mssql');

// config for your database
const config = {
    user: 'mywindows username',
    password: 'my windows password',
    server: '(localdb)\\v11.0', 
    database: 'test',
    options: {
        encrypt: true
      } 
};
console.log('starting sql');

var connection = new sql.connect(config, function(err) {
    console.log(err);
    var request = new sql.Request(connection); 
    request.query('select * from employees', function(err, recordset) {
       if(err)      // ... error checks 
            console.log('Database connection error');

    console.dir("User Data: "+recordset);
    });
});
sql.close();
console.log('ending sql');    
});

app.listen(3002, () => {
    console.log('Listening on port 3002');})

下面是错误消息

  

{ConnectionError:无法在(localdb)上查找实例-   getaddrinfo ENOTFOUND(localdb)       在Connection.tedious.once.err(C:\ Users \ vndbsubramaniam \ Desktop \ React   projects \ ReactWithSql \ node_modules \ mssql \ lib \ tedious.js:244:17)       在Object.onceWrapper(events.js:285:13)       在Connection.emit(events.js:197:13)       在InstanceLookup.instanceLookup(C:\ Users \ vndbsubramaniam \ Desktop \ React   项目\ ReactWithSql \ node_modules \乏味\ lib \ connection.js:945:16)       在sender.execute(C:\ Users \ vndbsubramaniam \ Desktop \ React projects \ ReactWithSql \ node_modules \ tedious \ lib \ instance-lookup.js:66:13)       在GetAddrInfoReqWrap.invokeLookupAll [作为回调](C:\ Users \ vndbsubramaniam \ Desktop \ React   项目\ ReactWithSql \ node_modules \乏味\ lib \ sender.js:43:16)       在GetAddrInfoReqWrap.onlookupall [按完成](dns.js:70:17)代码:'EINSTLOOKUP',originalError:{ConnectionError:Failed to   (localdb)上的查找实例-getaddrinfo ENOTFOUND(localdb)          在ConnectionError(C:\ Users \ vndbsubramaniam \ Desktop \ React projects \ ReactWithSql \ node_modules \ tedious \ lib \ errors.js:13:12)          在InstanceLookup.instanceLookup(C:\ Users \ vndbsubramaniam \ Desktop \ React   项目\ ReactWithSql \ node_modules \乏味\ lib \ connection.js:945:32)          在sender.execute(C:\ Users \ vndbsubramaniam \ Desktop \ React projects \ ReactWithSql \ node_modules \ tedious \ lib \ instance-lookup.js:66:13)          在GetAddrInfoReqWrap.invokeLookupAll [作为回调](C:\ Users \ vndbsubramaniam \ Desktop \ React   项目\ ReactWithSql \ node_modules \乏味\ lib \ sender.js:43:16)          在GetAddrInfoReqWrap.onlookupall [作为oncomplete](dns.js:70:17)        信息:         '无法在(localdb)上查找实例-getaddrinfo ENOTFOUND(localdb)',        代码:'EINSTLOOKUP'},名称:'ConnectionError'}数据库连接错误

2 个答案:

答案 0 :(得分:0)

经过数小时的努力,终于在这里SQL to Node connection

找到了答案。

似乎我必须添加msnodesqlv8软件包并使用将驱动程序语法添加到配置中。

app.get('/test', (req, res) => {

const sql = require('mssql/msnodesqlv8');

// config for your database
const config = {
    database: 'test',
    server: '(localdb)\\v11.0',
    driver: 'msnodesqlv8',
    options : {
        trustedConnection : true
    }
};
console.log('starting sql');

const pool = new sql.ConnectionPool(config);
pool.connect().then(() => {
    //simple query
    pool.request().query('select * from employees', (err, result) => {
          if(err) res.send(err)
          else{
              return res.json({
                  data : result.recordset
              })
          }
      })
      sql.close();
})    
console.log('ending sql');    

});

答案 1 :(得分:0)

您将需要msnodesqlv8驱动程序,您必须将其粘贴到require作为

var sql = require('mssql/msnodesqlv8'),

,以及您必须将其包含在配置对象的驱动程序部分中。

var config = {
    user:"*****",
    password:"*****",
    database:"*****",
    driver: 'msnodesqlv8',
    server:"*****",
    options: {
        trustedConnection : true
        }
}