ADO.Net 之SQLite批量Insert

sqlite普通方法插入:

        /// 
        /// 执行sql
        /// 
        /// 
        public void ExcuteSql(string sql)
        {
            if (openConn())
            {
                System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
                cmd.CommandText = sql;
                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
                conn.Close();
                conn.Dispose();
            }

        }


Sqlite利用事务批量Insert:

    public void inset()
        {
            using (DbConnection conn = DbProviderFactories.GetFactory("System.Data.SQLite").CreateConnection())
            {
                conn.ConnectionString = "Data Source = " + datasource + ";Version = 3";
                using (SQLiteCommand insertRngCmd = (SQLiteCommand)conn.CreateCommand())
                {
                    insertRngCmd.CommandText = @"INSERT INTO person (id, name) VALUES  (@id, @name)";
                    conn.Open();
                    var transaction = conn.BeginTransaction();
                    for (int i = 0; i < 10000; i++)
                    {
                        insertRngCmd.Parameters.AddWithValue("@id", i);
                        insertRngCmd.Parameters.AddWithValue("@name", "三");
                        insertRngCmd.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
            }
        }


后者万级控制在秒,前者万级性能极差要几分钟.呵呵,可能不算是批量吧,但性能还不错。刚刚接触SqlLie....

你可能感兴趣的:(SqlLite,sqlite,insert,sql,string,cmd)