网络编程
位置:首页>> 网络编程>> 数据库>> C#/.Net 中快速批量给SQLite数据库插入测试数据

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

作者:junjie  发布时间:2024-01-27 02:19:36 

标签:C#,.Net,快速,批量,SQLite,插入,数据

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

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com