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

12,148

Solution 1

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);

Solution 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);
Share:
12,148

Related videos on Youtube

VoidKing
Author by

VoidKing

I am currently an IT specialist for a municipal organization in the state of Oklahoma, and while I perform all sorts of IT specific tasks, my projects almost always include programming/web development. Since I started (a newb fresh out of college) I have learned a massive amount about programming best practices and functionality. I very much have Stack Overflow to thank for a lot of this. I have come to be an avid believer of Stack Exchange and their original goals (goals I think they are accomplishing very well, I might add). As a programmer, who constantly has to try new things, Stack Overflow has proven an invaluable resource for me. I try, research, try again, delve into old school books, and try again, but when push comes to shove, there are many times when my current level of experience in whatever language, proves inadequate and leaves me scratching my head, so to speak. It seems, that when I am in my darkest hour of coding, Stack Overflow (i.e., the community therein) not only gets me through the problem, but also teaches me how I can handle similar situations in the future, thus always making me a better programmer all around. :) I enjoy programming more than any other IT related task. Guess that's just who I am, but I suppose I am preaching to the choir here.

Updated on June 04, 2022

Comments

  • VoidKing
    VoidKing almost 2 years

    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.

    • Vladimirs
      Vladimirs
      With that approach is better to use separator that unlikely can be in one of array value like "|||" or something like that.
  • VoidKing
    VoidKing about 11 years
    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
    VoidKing about 11 years
    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?
  • Manish Mishra
    Manish Mishra about 11 years
    basically idea is to join your strings in a manner, that you can later, split it easily to get your original string array
  • VoidKing
    VoidKing about 11 years
    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
    VoidKing about 11 years
    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
    VoidKing about 11 years
    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
    VoidKing about 11 years
    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
    VoidKing about 11 years
    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
    VoidKing about 11 years
    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.
  • alvarorahul
    alvarorahul about 11 years
    this Wikipedia article explains how to create a foreign key en.wikipedia.org/wiki/Foreign_key
  • 27k1
    27k1 almost 3 years
    I know this is old, but please don't use binary formatter, it is now classified as unsafe code.