Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Insert 20,000 records with NHibernate

Insert 20,000 records with NHibernate

Scheduled Pinned Locked Moved C#
database
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Albu Marius
    wrote on last edited by
    #1

    I am using NHibernate to insert into a database 20,000 records. I would like to know if a time of 15 sec is good or bad and if my method is the best. The code that inserts is this:

        private void btnInsertHuge\_Click(object sender, EventArgs e)
        {
            ITransaction tx = null;
            ISession session = null;
    
            try
            {
                session = factory.OpenSession();
                tx = session.BeginTransaction();
    
                InsertHuge(session);
    
                tx.Commit();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                throw ex;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        ...........................
    
        private void InsertHuge(ISession session)
        {
            for (int i = 0; i < 20000; i++)
            {
                Person person = new Person();
                person.LastName = i.ToString();
                person.FirstName = i.ToString();
                person.PhoneNb = i.ToString();
    
                Car car = new Car();
                car.LicenseNumber = i.ToString();
                car.Name = i.ToString();
                car.Owner = person;
    
                session.SaveOrUpdate(car);
            }
         }
    

    NOTE: This is just a test. :)

    L 1 Reply Last reply
    0
    • A Albu Marius

      I am using NHibernate to insert into a database 20,000 records. I would like to know if a time of 15 sec is good or bad and if my method is the best. The code that inserts is this:

          private void btnInsertHuge\_Click(object sender, EventArgs e)
          {
              ITransaction tx = null;
              ISession session = null;
      
              try
              {
                  session = factory.OpenSession();
                  tx = session.BeginTransaction();
      
                  InsertHuge(session);
      
                  tx.Commit();
              }
              catch (Exception ex)
              {
                  tx.Rollback();
                  throw ex;
              }
              finally
              {
                  if (session != null)
                      session.Close();
              }
          }
          ...........................
      
          private void InsertHuge(ISession session)
          {
              for (int i = 0; i < 20000; i++)
              {
                  Person person = new Person();
                  person.LastName = i.ToString();
                  person.FirstName = i.ToString();
                  person.PhoneNb = i.ToString();
      
                  Car car = new Car();
                  car.LicenseNumber = i.ToString();
                  car.Name = i.ToString();
                  car.Owner = person;
      
                  session.SaveOrUpdate(car);
              }
           }
      

      NOTE: This is just a test. :)

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      As you adding a Car relation to Person, you are actually inserting 40 000 objects (i think ;p)

      **

      xacc.ide-0.2.0.77 - now with C# 3.5 support and Navigation Bar!^
      New xacc.ide release RSS feed^

      **

      A 1 Reply Last reply
      0
      • L leppie

        As you adding a Car relation to Person, you are actually inserting 40 000 objects (i think ;p)

        **

        xacc.ide-0.2.0.77 - now with C# 3.5 support and Navigation Bar!^
        New xacc.ide release RSS feed^

        **

        A Offline
        A Offline
        Albu Marius
        wrote on last edited by
        #3

        Yeah...:doh:you are right...my bad. But the question remains.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups