简体   繁体   中英

C# MySql Storing an arraylist in the database

I have an mysql database in which I am storing details of a project. One of these details is the staff associated with the project. In the project table I have a column for staff in the project. In my c# code behind I am storing it in an arraylist. I am wondering is there any way to store that arraylist in a single column in the database?

Thanks

You shouldn't store bunch of entities in a single column.

You'd better create another table and link your first one with second one through relation table.

TableA: id | name
TableB: id | bla
TableRelations: tableA_id | tableB_id

It is called many-to-many relation ( N:M as a shorthand)

First random link from google related to many-to-many

Generally storing multiples values in column is an indication of poor database design. It makes it very difficult to efficiently select rows based on criteria within that single column. Having said that, if you really only ever need to select those values on a per row basis then consider using XML

Using XML with MySQL

I think you should certainly follow the concept of many-to-many for this, like pointed by @zerkms. But if you is really interested in saving the ArrayList into a single field , you can serialize this ArrayList to String and store in a VARCHAR field .

About serializing ArrayList to String , you can view some suggested ways in this topic: Dynamically create javascript array with c# code behind .

Remember that you'll need to deserialize when reading back to your application too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM