当前位置:主页 > 软件编程 > .NET代码 >

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

时间:2021-05-30 08:47:22 | 栏目:.NET代码 | 点击:

使用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);
      }

您可能感兴趣的文章:

相关文章