【问题标题】:Bulk inserting with Node mssql package使用 Node mssql 包批量插入
【发布时间】:2017-09-25 13:23:15
【问题描述】:

我正在尝试从 csv 文件同时在我的 MSSQL Server 中插入大约 20.000 个用户。我为我的 Angular2 应用程序创建了一个 API,并将 csv 文件解析为 JSON 格式。但是现在我有点卡住了,我从 2 岁多的孩子那里找到了答案,所以这不再起作用了。有什么帮助吗?

这是我已经在使用的代码:

//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);

【问题讨论】:

  • 你为sql.SmallDateTime数据类型提供一个字符串:"GetDate()",但必须有Date的实例,例如:new Date()
  • 但是 GetDate() 是一个必须在 SQL 服务器本身上运行的函数?就像在查询中一样,您可以将 GetDate() 作为变量,它将当前日期放在表中?这就是我必须达到的目标。有什么想法吗?
  • 如果您不想提供new Date(),您可以在架构中设置默认值,例如:CREATE TABLE ...... DEFAULT GETDATE() 或编写原始 SQL 查询。
  • 不,您提供的字符串不会在服务器上运行。它由 node-mssql 库处理。
  • 天哪,我一直认为 Date() 实例的格式不正确……非常抱歉!非常感谢您的帮助!!!

标签: javascript node.js api node-mssql


【解决方案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 }

【讨论】:

  • 我收到此错误:TypeError: parameter.value.getTime is not a function,我猜这是因为我必须使用 SQL 函数 getDate()。有任何想法吗?提前致谢!
  • 如果parameter.value,请检查类型。猜猜,您希望它是Date,但事实并非如此。要检查它,请使用:XXXX instanceof Date。或者只是将其值打印到控制台。
  • 我并没有真正遵循您要我检查的内容,如果您不想看,我将在原始问题中添加我正在尝试的代码?提前致谢!
  • 您如何将其与ConnectionPool 一起使用? new connectionPool.Request() 是我的第一个猜测,但没有奏效。
  • 顺便说一句,当您调用const request = new sql.Request(); 时,提供connectionPool 对象作为参数const request = new sql.Request(connectionPool);
猜你喜欢
  • 1970-01-01
  • 2014-12-08
  • 2014-07-23
  • 1970-01-01
  • 2018-05-22
  • 2017-07-17
  • 2018-06-17
  • 2018-04-08
  • 2013-11-14
相关资源
最近更新 更多