Sqlite 快速批量插入数据 测试


 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;
        }

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM