【发布时间】:2021-09-27 07:39:44
【问题描述】:
我正在 NodeJS 中编写一个脚本来迭代一个 excel 文件,对其执行一些操作,然后在 MSSQL 中插入多行(每个 excel 行多次插入)。这意味着我必须遍历文件并发送 SQL 语句以针对我的繁琐连接执行。
我经常遇到这个错误:
请求只能在 LoggedIn 状态下进行,不能在 SentLogin7WithStandardLogin 状态下进行
wb.xlsx.readFile(filePath).then(function() {
var sh = wb.getWorksheet('Sheet Name');
// We start from row 11, the previous ones are not relevant
for (i = 11; i <= sh.rowCount; i++) {
// Iterate over each cell
for (j = 1; j <= sh.getRow(i).cellCount; j++) {
if (sh.getRow(10).getCell(j).value == 'X') {
// Build the query
let query = `INSERT INTO ...`;
// Create the request
const request = new Request(query, function (err) {
if (err)
throw err;
}
);
// Execute the statement
connection.execSql(request);
}
}
}
一次只能在一个连接上执行一个查询。您需要等到请求回调被执行,无论是错误还是结果,然后再发出另一个请求。
循环的第一次迭代正常工作。它将行插入到数据库中,但是当下一个尝试执行时,我收到了上一个错误。
实现这一目标的最佳方法是什么?我试图捕捉 requestCompleted 事件并在回调中使用 connection.reset(),但显然这不起作用。
【问题讨论】:
标签: javascript node.js tedious