我试图从一个 csv 文件中同时在我的 MSSQL 服务器中插入大约 20.000 个用户。我为我的 Angular2 应用程序创建了一个 API,并将 csv 文件解析为 JSON 格式。但是现在我有点卡住了,我从 2 岁以上的 child 那里找到了答案,所以它不再起作用了。有什么帮助吗?
这是我已经在使用的代码:
//Insert gebruikers from CSV file
router.post('/gebruikers/csv', type, (req, res) => {
fs.readFile(req.file.path, 'utf8', function (err, csvData) {
if (err) {
res.send("error in file reader: " + err);
return console.log(err);
}
csvParser(csvData, {
delimiter: ',',
columns: true
}, function (err, json) {
if (err) {
res.send("error in csvParser: " + err);
console.log(err);
} else {
console.log(json);
//I think the query should be done here...
}
})
})
});
提前致谢!
编辑
现在使用此代码但它返回此错误:
TypeError: parameter.value.getTime is not a function
代码:
router.post('/gebruikers/csv', type, (req, res) => {
fs.readFile(req.file.path, 'utf8', function (err, csvData) {
if (err) {
res.send("error in file reader: " + err);
return console.log(err);
}
csvParser(csvData, {
columns: true,
ltrim: true,
rtrim: true
}, function (err, json) {
if (err) {
res.send("error in csvParser: " + err);
console.log(err);
} else {
console.log(json);
const table = new sql.Table('[Alg].[User]');
table.create = true;
table.columns.add('Firstname', sql.NVarChar, { nullable: false });
table.columns.add('LastName', sql.NVarChar, { nullable: false });
table.columns.add('PrivateMail', sql.NVarChar, { nullable: false });
table.columns.add('UserName', sql.NVarChar, { nullable: false });
table.columns.add('Password', sql.NVarChar, { nullable: false });
table.columns.add('Auth', sql.NVarChar, { nullable: false });
table.columns.add('Enabled', sql.Bit, { nullable: false });
table.columns.add('Created', sql.SmallDateTime, { nullable: false });
table.columns.add('Manual', sql.Bit, { nullable: false });
table.columns.add('LastChanged', sql.SmallDateTime, { nullable: false });
table.columns.add('Staff', sql.Bit, { nullable: false });
var count = Object.keys(json).length;
console.log(count);
for (i = 0; i < count; i++) {
table.rows.add(json[i].Firstname, json[i].LastName, json[i].PrivateMail, json[i].UserName, json[i].Password, 'manual', 1, "GetDate()", 1, "GetDate()", 1);
}
console.log("Async function started!");
const request = new sql.Request();
request.bulk(table, (err, result) => {
// error checks?
if (err) {
console.log("ERROR post bulk gebruikers: " + err);
}
else {
res.send("SUCCES! " + result);
}
})
}
})
})
});
已修复
正如用户 Grigoriy Chudnov 所指出的,我需要一个 new Date()
的实例。我当时认为这行不通,因为格式错误,但 node-mssql 为我处理了这个问题。
currentDateTime = new Date();
table.rows.add(json[i].Firstname, json[i].LastName, json[i].PrivateMail, json[i].UserName, json[i].Password, 'manual', 1, currentDateTime, 1, currentDateTime, 1);
最佳答案
如果您使用 node-mssql图书馆,使用 bulk insert一次添加多条记录。
它应该看起来像这样:
'use strict';
const sql = require('mssql');
const user = 'sa';
const password = 'yourStrong(!)Password';
const connStr = `mssql://${user}:${password}@172.17.0.2:1433/tempdb?encrypt=true`;
sql.connect(connStr)
.then(() => {
console.log('connected');
const table = new sql.Table('my_users');
table.create = true;
table.columns.add('id', sql.Int, { nullable: false, primary: true });
table.columns.add('name', sql.VarChar(128), { nullable: false });
// add here rows to insert into the table
table.rows.add(1, 'Alice');
table.rows.add(2, 'Bob');
table.rows.add(3, 'Carol');
const request = new sql.Request();
return request.bulk(table)
})
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
});
输出是:
connected
{ rowsAffected: 3 }
关于javascript - 使用 Node mssql 包批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663017/