欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#/.Net 中快速批量给SQLite数据库插入测试数据

程序员文章站 2024-01-27 21:45:04
使用transaction: var stopwatch = new stopwatch(); using (var cmd = new sqli...

使用transaction:

 var stopwatch = new stopwatch();
      using (var cmd = new sqlitecommand(db_con))
      using (var transaction = db_con.begintransaction())
      {
        stopwatch.reset();
        stopwatch.start();
        foreach (var item in sorted)
        {
          sql = string.format("insert into db (st1, st2) values ('{0}', {1})", item.key.replace("'", "''"), item.value);
          cmd.commandtext = sql;
          cmd.executenonquery();
          ++readcnt;
          if (++readcnt % 1000000 == 0)
          {
            console.write("\rdumped {0} lines...", readcnt);
          }
        }
        console.write("\rcommitting....");
        transaction.commit();
        stopwatch.stop();
        console.write("\rdumped {0} lines using {1} seconds...", readcnt, stopwatch.elapsed.totalseconds);
      }