7

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.

VoidKing
  • 6,282
  • 9
  • 49
  • 81
  • why don't you simply `var a = String.Join(",",arrays);` and store a in db? and fetch it back like `var arr= a.Split(",");` – Manish Mishra Apr 04 '13 at 13:57
  • 1
    With that approach is better to use separator that unlikely can be in one of array value like "|||" or something like that. – Vladimirs Apr 04 '13 at 13:59
  • Try to adapt this sample: http://sqlcebulkcopy.codeplex.com – KF2 Apr 04 '13 at 14:00
  • @ManishMishra Is there anyway you could post as an answer? It seems like a really good approach and deserves to be an accepted answer, if it works (I don't see why it wouldn't, as long as something more like "|||" was used as a separator, instead of "," as Vladimirs suggested). – VoidKing Apr 04 '13 at 14:16

3 Answers3

11

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);
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
  • Can you edit using "," as the separator, as that value could easily be natural text in the strings themselves, as they are stored from user input? – VoidKing Apr 04 '13 at 14:31
  • basically idea is to join your strings in a manner, that you can later, split it easily to get your original string array – Manish Mishra Apr 04 '13 at 14:33
  • Right, of course, I get that, I was just thinking of future viewers, but I suppose it's true that if they don't get that they should use a separator unique from their string values, there probably is no helping them, anyway, lol. Thanks for the answer, it seems obvious now, but I knew I had to make sure that my answer didn't violate any best practices or anything. – VoidKing Apr 04 '13 at 14:37
  • One thing, though, you may want to change your example code when using "Split()" that throws an error ("best overloaded method for Split has some invalid arguments"). – VoidKing Apr 04 '13 at 14:44
  • Well, I say that, but after looking up the syntax I'm seeing pretty much what you're showing me, so I guess it's time to look at "my" values :) – VoidKing Apr 04 '13 at 14:47
  • Oh, I get what's going on, "Split()" only takes a single "char" as a value. I wonder how to "Split" on more than one char... – VoidKing Apr 04 '13 at 14:49
2

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);
Flavia
  • 563
  • 4
  • 9
  • Can you show an example of deserialization using this method. Also, just to be sure, is this answer okay to use for sql-server-ce (using WebMatrix)? – VoidKing Apr 04 '13 at 14:03
  • Finally got a chance to look at this. +1 for doing this through serialization/deserialization (concatenated strings may not work for everybody). I'd give another +1 if I could for showing me new methods I didn't know about! Thanks! – VoidKing Apr 05 '13 at 16:24
  • I know this is old, but please don't use binary formatter, it is now classified as unsafe code. – 27k1 May 01 '21 at 08:01
1

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

  • I was wondering if I was overlooking that. I do know what a 1 to many relationship is, but I haven't practiced that approach in so long. It would probably be beneficial to show an example of that here (for future viewers). This is a really good answer, considering the scenario. – VoidKing Apr 10 '13 at 20:02
  • this Wikipedia article explains how to create a foreign key http://en.wikipedia.org/wiki/Foreign_key – alvarorahul Apr 10 '13 at 21:09