0

使用 Tedious 连接到 MSSQL DB。在下面的代码中,我收到错误:

RequestError: Requests can only be made in the LoggedIn state, not the Connecting state

exports.exec_sql_cmd = async function(cmd)
{
var config = {  
    server: 'xx.xx.xx.xx',
    authentication: {
        type: 'ntlm',
        options: {
            userName: 'master',
            password: 'masterp',
   domain: 'the.domain.com'
        }
    },
    options: {            
        encrypt: false,
        database: 'tblTest'
    }
};  
var connection = new Connection(config);
connection.on('connect', function(err) { 
    console.log("Connected");  
    executeStatement();
});
connection.connect();
 
function executeStatement() {  
var request = new Request(cmd, function(err) {  
 if (err) {  
    console.log(err);}  
});  

request.on('row', function(columns) {  
    columns.forEach(function(column) {  
      if (column.value === null) {  
        console.log('NULL');  
      } else {  
        console.log("Row Inserted");  
      }  
    });  
});

request.on("requestCompleted", function (rowCount, more) {
    connection.close();
});
connection.execSql(request);  
}

}

`

我确实已连接(此行运行console.log("Connected");:),但随后引发此错误: RequestError: Requests can only be made in the LoggedIn state, not the Connecting state

我也在下面尝试过这种方式,但这会引发错误:ERROR ConnectionError: Failed to connect to xx.xx.xx.xx:1433 in 15000ms

var connection = new Connection(config);
connection.on('connect', function(err) { 
    console.log("I Am Connected!");  
});

connection.connect((err) => {
    if (err)
      return callback(err, null);

    const request = new Request(sql, (rerr, rowCount, rows) => {
        
      if (rerr)
        {
            console.log("THIS IS THE ERROR: " + rerr)
            return callback(rerr, null)
        }

        callback(null, {rowCount, rows});
    });

    request.on("requestCompleted", function (rowCount, more) {
        connection.close();
    });

    connection.execSql(request);
  });

提前致谢。

4

0 回答 0