[英]Having issues with connection when using node-mssql connected with Azure sql database
[英]node-mssql error message when trying to do a bulk insert (While reading current row from host, a premature end-of-message was encountered)
在 node.js 中嘗試使用node-mssql
lib 的.bulk
操作時,我收到消息:
While reading current row from host, a premature end-of-message was encountered--an incoming data stream was interrupted when the server expected to see more data. The host program may have terminated. Ensure that you are using a supported client application programming interface (API)
// the problem happens when i call this function
export async function insert(
transaction: sql.Request,
data: any[],
) {
const table = prepare(data);
return await transaction.bulk(table);
}
function prepare(data: any[]) {
const table = new sql.Table('dbo.SomeTable');
table.create = false;
table.columns.add('MyColumn1', sql.Int, { nullable: false });
table.columns.add('MyColumn2', sql.Int, { nullable: false });
table.columns.add('MyColumn3', sql.Int, { nullable: false });
for (const mov of data) {
table.rows.add(
mov.field1,
mov.field2,
mov.field3
);
}
return table;
}
經過一段時間搜索解決方案后,我發現問題出在我的data
數組是空的。 我在這個鏈接中找到了解決方案: https://github.com/tediousjs/tedious/issues/212#issuecomment-628330044
我之前遇到過幾個這樣的錯誤。 當您沒有在循環中插入任何行 > 時,也會發生這種情況。 一個典型的情況是您添加了列,然后從其他地方獲取一些數組 > 並迭代該數組以插入行。 有時,當數組為空時,沒有插入任何行,我會得到這個錯誤。 因此,最好在執行批量加載之前檢查數組是否為空。 (作者jianingliu ,Github)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.