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