user175084
user175084

Reputation: 4630

arraylist to a table

I have an arraylist called backuplist.

this arraylist has structures in it.

So what i need to do is transfer this arraylist in a table and then store this table in my SQL database.

Anybody with ideas as to what i should do..?? Even if it is a different way to do this please let me know.

Thanks

Upvotes: 2

Views: 2184

Answers (4)

James
James

Reputation: 330

I've been using an ArrayList returned by MySQL so it's populated with column names and types etc.

ArrayList list = new ArrayList();
// Add Items to list
DataTable table = new DataTable();
table.Load(list);

Upvotes: 0

Bob Palmer
Bob Palmer

Reputation: 4762

I'd agree on using a strongly typed list as Marc suggested. Another option to getting those into the database would be to plow through with a foreach (on either your list or array), and use the structure properties as parameters to an insert stored procedure.

We do this all the time in our app, where we might have a List coming from a business component and being tossed to the data layer, where we'll loop through, do any necesary manipulation, then run our update SP on each row.

Let me know if you need a snippet.

-Bob

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062905

If you are using VS2008 (tags), you should ideally use List<T>, not ArrayList. You can convert from a List<T> to a DataTable like so; then just use a SqlDataAdapter or SqlBulkCopy to get the data into the database.

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180808

This isn't a complete solution, but I thought I'd point you in the right direction. The problem is I don't really know your application, and your experience is limited, so it's going to be a stab in the dark.

Anyway, here are a couple of resources to get you started:

Converting Custom Collections To and From DataTable
http://blog.lozanotek.com/archive/2007/05/09/Converting_Custom_Collections_To_and_From_DataTable.aspx

Inserting New Records into a Database
http://msdn.microsoft.com/en-us/library/ms233812(VS.80).aspx

Upvotes: 0

Related Questions