what is the best Approach to save a List Object?

Last post 03-25-2009 10:22 AM by mglil. 7 replies.

Sort Posts:

  • what is the best Approach to save a List Object?

    03-20-2009, 9:45 AM
    • Member
      288 point Member
    • sahajMarg
    • Member since 05-03-2007, 6:59 PM
    • Posts 583

    what is the best Approach to save a List Object?

    Lets say I have a custom Object Person and then I have a Person Collection Object which is basically a List or a collection of Person.

    Now I want to save the collection object into Database.

    Should I loop though the entire collection object and save one person by peron into the database? or is there any other method?

    Thanks!

  • Re: what is the best Approach to save a List Object?

    03-20-2009, 9:57 AM
    Answer
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
      TrustedFriends-MVPs

    I would loop through the collection just as you have suggested and save them one by one, but maybe someone has a better idea.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: what is the best Approach to save a List Object?

    03-20-2009, 10:05 AM
    Answer
    • Contributor
      2,236 point Contributor
    • mohsinaeem
    • Member since 05-08-2008, 7:41 AM
    • Fort Lauderdale, FL
    • Posts 336

    You can save both the list and object into the database. It depends upon the scenario, in which you want to use it. If you in future you need complete list then save list into the database. But if you want individual person object then save them separately. See this thread how to save objects:

    http://www.gamedev.net/community/forums/topic.asp?topic_id=494225

    Mohsin Naeem
    MCPD 2.0, MCTS (SharePoint 2007, SQL Server 2005)
    "Please mark as answer if it helped you"
  • Re: what is the best Approach to save a List Object?

    03-20-2009, 1:57 PM
    • Member
      288 point Member
    • sahajMarg
    • Member since 05-03-2007, 6:59 PM
    • Posts 583

    Thanks for your response ecbruck.

    I was thinking of converting my list object into a DataTable and then use the   SqlDataAdapter.Update(dataTable)  Method.

    Do you have any idea about that?

    Thanks again!

  • Re: what is the best Approach to save a List Object?

    03-23-2009, 3:54 AM
    Answer

    Another way is to serialize the list object to xml and pass the xml to DB for single insert....

    Saravanan

  • Re: what is the best Approach to save a List Object?

    03-23-2009, 8:04 AM
    Answer
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
      TrustedFriends-MVPs

    sahajMarg:

    Thanks for your response ecbruck.

    I was thinking of converting my list object into a DataTable and then use the   SqlDataAdapter.Update(dataTable)  Method.

    Do you have any idea about that?

    Thanks again!

    I've used this method if I've started with a DataTable, but not otherwise. I'd have to test the differences in the two, but usually I'd simply loop through the collection.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: what is the best Approach to save a List Object?

    03-23-2009, 8:21 AM
    Answer

    sahajMarg:
    Should I loop though the entire collection object and save one person by peron into the database? or is there any other method?

    If you are going to loop and save each person individually ensure you wrap it in a transaction so that if there is an exception half way through saving you can roll back to a known state and avoid having some people saved and others transient.

    Introducing System.Transactions in the .NET Framework 2.0
    http://msdn.microsoft.com/en-us/library/ms973865.aspx

    Unit of Work Pattern
    http://martinfowler.com/eaaCatalog/unitOfWork.html

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: what is the best Approach to save a List Object?

    03-25-2009, 10:22 AM
    Answer
    • Member
      57 point Member
    • mglil
    • Member since 07-05-2006, 2:47 PM
    • USA
    • Posts 12

    For Custom Object, it could be better to loop in the list and save them one by one, instead of using the datatable, datatable has some good features but it comes with it's overhide too.

    Mglil
    http://www.mglil.com
Page 1 of 1 (8 items)