Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have an ArrayList:

ArrayList arrayc = new ArrayList();
     arrayc.Insert(0,"asd");
     arrayc.Insert(1,"wet");
     arrayc.Insert(2,"wertWER");


I want to store it in a database. How can I achieve this?
Posted
Updated 19-Jul-18 21:06pm
v2
Comments
Sandeep Mewara 13-Apr-11 6:35am    
How do you think it can be done? Thoughts? Ideas?

BTW, any thoughts on the table structure?
Tarun.K.S 13-Apr-11 6:37am    
I think you should start learning about querying databases and also to self-learning. Simply asking for code won't help you in the long run.

There are two ways: make each item in your ArrayList a separate row in a database (recommended) or serialize your ArrayList and store the resulting stream as binary data.

I would do the former: the later relies too much on the implementation of your program.

In addition, please try to avoid ArrayList: use the typesafe version List<T> instead. It promotes readability by restricting what can be put into the list, and does not require checking and casting when you take data out.
 
Share this answer
 
Comments
Costica U 13-Apr-11 6:46am    
Good point
Hi,

Consider creating an SQL Compact Edition database, which will store the database in a file but allow standard database access through an API like the entity framework. See this article:

http://technet.microsoft.com/en-us/library/cc835495.aspx[^]

This way a SQL CE database is distributed with your application.

Use the Entity Framework to create a model (i.e. the table structure) and create the database from this. You can then use the classes created to add data to your database or query data from it.
 
Share this answer
 
Another way I think is simple, is by appending the items to a single string using some symbol between the items(- or _ or &, etc) and store it in the database. While reading it, you can parse it to an ArrayList with a single line of code. I use this because sometimes, I don't know how many items there will be in the ArrayList to prepare columns. (Make sure you use a symbol that you're sure won't be present in any of your items).
 
Share this answer
 
Comments
CHill60 20-Jul-18 8:00am    
This is a truly awful way to go about things. Never concatenate things into a single string to store them - one item = one row on a table! If you don't know how many there are going to be it doesn't matter. Learn about Foreign Keys

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900