C#/.Net 中快速批量给SQLite数据库插入测试数据
程序员文章站
2023-11-24 19:18:52
使用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); }
下一篇: C#将布尔类型转换成字节数组的方法