无法与节点js中的MsSql数据库连接

时间:2018-10-17 05:33:32

标签: node.js sql-server connection

我无法与mssql服务器连接,执行编码时出现错误。

exports.dbConfig = {
    // user: "sa",
    // password: "sa",
    // //server: "DESKTOP-QHBGOMQ",
    // server: '127.0.0.1',
    // //server: "localhost",
    // database: "ArduinoDataLogger",
    // port: 1433,
    //
    // connectionString: "Driver={SQL Server Native Client 11.0};Server=#{server}\\sql;Database=#{database};Uid=#{user};Pwd=#{password};"
    userName: 'sa',
    password: 'sa',
    server: 'DESKTOP-QHBGOMQ',
    database: 'ArduinoDataLogger',
    port: 1443,
    debug: true,
    driver: 'tedious',
    options: {
        port: 1443,
        encrypt: false,
        database: 'ArduinoDataLogger',
        useColumnNames: false,
        debug: {
            packet: true,
            data: true,
            payload: true,
            token: true,
            log: true
        }
    }

};

我得到的错误是:

ConnectionError: Failed to connect to DESKTOP-QHBGOMQ:1443 - Could not connect (sequence)
    at Connection.tedious.once.err (C:\Users\JanaVithu\Desktop\NewProject\node_modules\mssql\lib\tedious.js:237:17)
    at Connection.g (events.js:292:16)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at Connection.socketError (C:\Users\JanaVithu\Desktop\NewProject\node_modules\tedious\lib\connection.js:1016:14)
    at C:\Users\JanaVithu\Desktop\NewProject\node_modules\tedious\lib\connection.js:861:25
    at SequentialConnectionStrategy.connect (C:\Users\JanaVithu\Desktop\NewProject\node_modules\tedious\lib\connector.js:154:9)
    at Socket.onError (C:\Users\JanaVithu\Desktop\NewProject\node_modules\tedious\lib\connector.js:170:16)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
  code: 'ESOCKET',
  originalError:

我已启用tcp / ip,并且我所有的mssql服务器都在运行(自动)。 我什至更改了tcp / ip动态= 0。 我真的不知道如何解决它,我已经尝试了专家(论坛)建议的所有可能的解决方案。

谢谢。 如果有人可以建议/帮助我解决此问题,那将是很好的。

1 个答案:

答案 0 :(得分:0)

使用这些设置对我有用。

const config = {
  user: {user},
  password: {password},
  dialect: "mssql",
  server: {server},
  port:1435,
  database: {database},
  debug: true,  
  dialectOptions: {
    instanceName: 'SQLEXPRESS'
  }
};
sql.connect(config,function(err){
    if (err) console.log(err);
    console.log("SQL DATABASE CONNECTED");  
});

编辑

exports.dbConfig = {
    user: 'sa',
    password: 'sa',
    dialect: "mssql",
    server: 'DESKTOP-QHBGOMQ',
    database: 'ArduinoDataLogger',
    port: 1443, // check the port once again
    debug: true,
    dialectOptions: {
      instanceName: 'SQLEXPRESS'
    }
};