Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to store an array into a single column in a SQL Server CE database?

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.

like image 495
VoidKing Avatar asked Apr 04 '13 13:04

VoidKing


2 Answers

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);
like image 154
Manish Mishra Avatar answered Oct 19 '22 04:10

Manish Mishra


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);
like image 44
Flavia Avatar answered Oct 19 '22 06:10

Flavia