尝试使用节点js连接SQL

时间:2018-02-05 06:40:36

标签: node.js sql-server-2012

app.get(' /',function(req,res){     var config = {         用户:' sa',         密码:'',         服务器:' localhost',         数据库:' TestDB'     };

// connect to your database
sql.connect(config, function (err) {

    if (err) console.log(err);

    // create Request object
    var request = new sql.Request();

    // query to the database and get the records
    request.query('select * from userInfo', function (err, recordset) {

        if (err) console.log(err)

        // send records as a response
        res.send(recordset);

        console.log(recordset);
    });
});

});

在命令提示符中获取此错误

{ Error: Failed to connect to localhost:1433 - Could not connect (sequence)
    at Connection.tedious.once.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:216:17)
    at Object.onceWrapper (events.js:293:19)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:191:7)
    at Connection.socketError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:875:14)
    at D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:740:25
    at SequentialConnectionStrategy.connect (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:153:9)
    at Socket.onError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:169:16)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
  code: 'ESOCKET',
  originalError:
   { ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
       at ConnectionError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\errors.js:12:12)
       at Connection.socketError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:875:30)
       at D:\Nodejs\UsersCreate\node_modules\tedious\lib\connection.js:740:25
       at SequentialConnectionStrategy.connect (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:153:9)
       at Socket.onError (D:\Nodejs\UsersCreate\node_modules\tedious\lib\connector.js:169:16)
       at emitOne (events.js:96:13)
       at Socket.emit (events.js:191:7)
       at emitErrorNT (net.js:1284:8)
       at _combinedTickCallback (internal/process/next_tick.js:80:11)
       at process._tickCallback (internal/process/next_tick.js:104:9)
     message: 'Failed to connect to localhost:1433 - Could not connect (sequence)',
     code: 'ESOCKET' },
  name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
    at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1299:37)
    at Request._query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\tedious.js:497:11)
    at Request.query (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:1242:12)
    at D:\Nodejs\UsersCreate\app.js:118:17
    at _poolCreate.then.catch.err (D:\Nodejs\UsersCreate\node_modules\mssql\lib\base.js:269:7)
    at process._tickCallback (internal/process/next_tick.js:109:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
undefined

请帮助我解决此错误,并需要导航以纠正此错误

0 个答案:

没有答案