Javascript 使用节点mssql包进行批量插入
我正试图从csv文件中同时在MSSQL服务器中插入大约20000个用户。我为Angular2应用程序创建了一个API,并将csv文件解析为JSON格式。但现在我有点被卡住了,我从两岁多的孩子那里找到了答案,所以这已经不起作用了。有什么帮助吗 这是我已经在使用的代码:Javascript 使用节点mssql包进行批量插入,javascript,node.js,api,node-mssql,Javascript,Node.js,Api,Node Mssql,我正试图从csv文件中同时在MSSQL服务器中插入大约20000个用户。我为Angular2应用程序创建了一个API,并将csv文件解析为JSON格式。但现在我有点被卡住了,我从两岁多的孩子那里找到了答案,所以这已经不起作用了。有什么帮助吗 这是我已经在使用的代码: //Insert gebruikers from CSV file router.post('/gebruikers/csv', type, (req, res) => { fs.readFile(req.file.
//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
代码:
如果使用库,请使用一次添加多条记录
它应该是这样的:
'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 }
我将mysql用于NodeJS,下面是我用于在单个事务中插入多条记录的代码:
let inserRows = [
['some content',1],
['some more content',2]
];
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'testing',
});
//connection is MySQL connection
connection.query('INSERT INTO Table_Name (Content,Id) VALUES ?;',[insertRows],
(err,result) => {
if (err) {
console.log(err.message);
};
console.log(result);
我得到了这个错误:
TypeError:parameter.value.getTime不是一个函数,我猜这是因为我必须使用SQL函数getDate()
。有什么想法吗?提前谢谢!!如果参数值
,请检查类型。猜一猜,您希望它是日期
,但事实并非如此。要检查它,请使用:XXXX instanceof Date
。或者只是把它的值打印到控制台上。我并没有真正按照你要我检查的内容去做,如果你不想看的话,我会在我的原始问题中添加我正在尝试的代码?提前谢谢!如何将其用于连接池
newconnectionpool.Request()
是我的第一个猜测,但没有成功。顺便说一句,当你调用const-Request=new-sql.Request()时,我找到了答案
,提供connectionPool对象作为参数,const request=new sql.request(connectionPool)
为sql.SmallDateTime
数据类型提供字符串:“GetDate()”
,但必须有Date
的实例,例如:new Date()但GetDate()是必须在sql server本身上运行的函数?就像在查询中一样,您可以将GetDate()作为变量,它会将当前日期放入表中?这就是我必须达到的目标。有什么想法吗?如果你不想提供newdate()
你可以在schema中设置默认值,例如:createtable。。。。。。默认GETDATE()
或编写原始SQL查询。否,您提供的字符串不会在服务器上运行。它是由节点mssql库处理的。哦,天哪,我一直在想Date()实例的格式可能不正确。。。我很抱歉!!非常感谢你的帮助!!!您发布了关于MySQL的文章,但问题是关于MSSQL的。你的解决方案根本不起作用!
connected
{ rowsAffected: 3 }
let inserRows = [
['some content',1],
['some more content',2]
];
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'testing',
});
//connection is MySQL connection
connection.query('INSERT INTO Table_Name (Content,Id) VALUES ?;',[insertRows],
(err,result) => {
if (err) {
console.log(err.message);
};
console.log(result);