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>();
}
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 1
    Loop through the list, and add each record to the MySQL table using an INSERT INTO statement. – Robert Harvey Apr 17 '13 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 – Boardy Apr 17 '13 at 17:52
  • What is the datatype of the MySQL field that you want to contain this list? – Chris Farmer Apr 17 '13 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 – Boardy Apr 18 '13 at 11:49

2 Answers2

2

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

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
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

djcrabhat
  • 464
  • 5
  • 10
  • 1
    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. – Chris Farmer Apr 17 '13 at 18:02
  • You're right, without knowing the exact details of the situation, breaking out this data to another table may not make any sense. – djcrabhat Apr 17 '13 at 18:06
  • @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 – Boardy Apr 17 '13 at 21:14