弹来弹去跑马灯!

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

  

posted @ 2018-08-02 14:56  wgscd  阅读(8879)  评论(0编辑  收藏  举报