ArrayList saved to SQL database?

Posted on 2006-05-18
Last Modified: 2008-02-10

I have a Session arraylist that I need to write each row into an SQL database:

ArrayList items = (ArrayList)Session["mycart"];

Q. How do you save an ArrayList to an SQL database?
Question by:kvnsdr
    LVL 20

    Expert Comment

    The best would be if you make several tables in the database: order, item. The order will be connected with the current user (one user can have multiple orders), item is connected to an order (one order can have multiple items). In the items table you will save all the properties of the item. Then you can go over the array list and save every item in the items table using an SqlCommand or an SqlDataAdapter
    LVL 1

    Author Comment

    So a Session DataSet is a better choice than an ArrayList?
    LVL 20

    Expert Comment

    Yes and no. The dataset will take more memory for every user on your site but will give you a flexible way to save in the database. So you have to chose: either use more memory and develop easily or use less memory and make transformations before you save the data

    Accepted Solution

    I'm going to make the following assumptions about the environment/design:

    - Each item in the ArrayList() is meant to be a row in the database
    - There is a stored procedure that will be called to actually perform the insert
    - Each item in the ArrayList() is an object containing something on the order of fields (i.e., multiple values of some kind)

    The psuedo-code for accomplishing this would look something like:

    using( SqlConnection Connection = GetSqlConnection() )
        SqlCommand Command = new SqlCommand( StoredProcedureName );
        Command.CommandType = CommandType.StoredProc;

        SqlParameterCollection Parameters = Command.Parameters;
        Parameters.Add( Field1 );
        Parameters.Add( Field2 );
        Parameters.Add( Field3 );
        // --- etc. ----
        foreach( LineItem in TheArrayList )
            Parameters[ Field1 ] = LineItem.Value1;
            Parameters[ Field2 ] = LineItem.Value2;
            Parameters[ Field3 ] = LineItem.Value3;
            // --- etc. ---


    There are a few more efficiencies that could be applied, but they will greatly increase the complexity. Odds are that it just wouldn't be worth it as there probably would be no perceivable performance impact.

    Let me know if any of my assumptions are incorrect and I will update this response accordingly.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now