1

I am working on a C# project and I have a List of type DbAndTables e.g.

List<DBAndTableNames> myList = new List<DBAndTableNames>();

I need to store the contents of this list in a MySQL Database but I can't find anything on Google on how to do this oddly. I think I have a slight recollection that I need to convert it to a byte array and then somehow this can be written to the database.

Thanks for any help you can provide.

Update I forgot to mention that this is a multi dimensional list array below is the class definition that the list uses. I've looked at the XMLSerialization but from what I've seen this doesn't support mutli dimensional lists.

public class DatabaseAndTableNames
{
    public string database;
    public List<string> tables = new List<string>();
}
4
  • 1
    Loop through the list, and add each record to the MySQL table using an INSERT INTO statement. Commented Apr 17, 2013 at 17:50
  • I don't want to add each individual array item in a single record as the array contains other arrays as well. I want the entirety of the array to be stored in one DB record Commented Apr 17, 2013 at 17:52
  • What is the datatype of the MySQL field that you want to contain this list? Commented Apr 17, 2013 at 17:57
  • @ChrisFarmer, that hasn't been decided yet as not sure how the list would be converted but at a guess I would assume it would need to be a BLOB Commented Apr 18, 2013 at 11:49

2 Answers 2

2

What you can do is serialize and save it on database. when you need it you can Deserialize it.

Sign up to request clarification or add additional context in comments.

Comments

0

This is kind of a funky way to do it. You should really save it as a one-to-many relationship in a second table. But if you REALLY want to save the contents of an array to a single column in the DB, you'll have to serialize the data to text in some way (XMLSerializer is nice and easy to use) Or if you're just storing a list of strings, create a pipe or command delimited list and save that to the column

3 Comments

I'm not sure that it's true that "you should really save it as a one-to-many relationship." It totally depends on what he's planning to do with the data. If he never needs to walk that relationship through database queries and just needs to persist or retrieve it as a single bundle, his request seems legit.
You're right, without knowing the exact details of the situation, breaking out this data to another table may not make any sense.
@ChrisFarmer your right, for what I want there is no point adding each item across multiple tables instead of storing the object itself. I will always need everything from the list not sub parts of it so storing each item in a row would be pointless for me hence why I want the entireity of the list to be stored in one row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.