Reputation: 6412
I have reviewed possible answers here (for PHP, I think): http://www.lateralcode.com/store-array-database/ but I am unable to find a C#.net version of serialize/deserialize.
Would this be done the same as the way shown in my link, above, or is there a completely different approach I should be using, given the environment?
I just don't want to have a bunch of different columns for each of the 12 values in each of my 9 different arrays, so if there is another approach to achieve this (converting to byte[], etc.) I am more than willing to hear it.
If it helps any, the arrays will be simple string[] arrays.
Upvotes: 7
Views: 14229
Reputation: 89
I just don't want to have a bunch of different columns for each of the 12 values in each of my 9 different arrays, so if there is another approach to achieve this (converting to byte[], etc.) I am more than willing to hear it.
From the above description, it looks like you are using an RDBMS.
The fact that you want to store multiple values in a single column screams of an issue with the design.
I concur that having separate columns may not be the way to go, especially if the number of items in each array could potentially change in the future.
Consider separating this data into a separate table and having a 1 to many mapping with your original table with a foreign key relationship
Upvotes: 1
Reputation: 561
Try this to seralize the array and create a column in the database of type Blob to store the byte array.
Serialization:
if(array == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, array);
Deserialization:
String[] array = new String[10];
BinaryFormatter bf = new BinaryFormatter();
ms.Position = 0;
array = (String[])bf.Deserialize(ms);
Upvotes: 2
Reputation: 12375
Convert your string array into single String like given below:
var a = String.Join(",",arrays);
//or aim is to provide a unique separator,
//i.e which won't be the part of string values itself.
var a= String.Join("~~",arrays);
and fetch it back like this:
var arr = a.Split(',');
//or split via multiple character
var arr = a.Split(new string[] { "~~" }, StringSplitOptions.None);
Upvotes: 11