Sqlite 快速批量插入数据 测试

时间:2022-11-12 14:47:57
 public static int insertDbBatch()
{ string sql = ""; SQLiteConnection conn = new SQLiteConnection(@"Data Source=" + dbFile + ";Initial Catalog=sqlite;Integrated Security=True;");
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(conn);
DbTransaction trans = conn.BeginTransaction(); // <------------------- int cnt = 0;
try
{ for (int i = 1; i <= 50000; i++)
{ // sql = "insert into members (uid,hashCode,nickName) select '" + rnd.Next(11111111, 999999999) + "','hash" + rnd.Next(11111111, 999999999) + "','测试数据'";
sql = "insert into members (uid,hashCode,nickName) select '" + Guid.NewGuid() + "','hash" + rnd.Next(11111111, 999999999) + "','测试数据'";
cmd.CommandText = sql;
cnt += cmd.ExecuteNonQuery(); } trans.Commit();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
finally
{ if (conn.State == ConnectionState.Open)
{
conn.Close();
} } return cnt;
}