0

I have a vb .net ArrayList that I would like to store in one field on my Ms SQL 2008 database. Is it possible to put it in and get it back out as an ArrayList?

I'm guessing i need to break down the Array some how and store it as a string, then rebuild it when reading back as i dont see any datatypes for sql that are for arrays. Which is a bummer for me!

Can anyone suggest the best way to do this?

Many thanks.

edit - The ArrayList stores a set of quote ID's. There is no limit to how big it is so it could hold 1 or 1001 different ID's

asked Mar 21, 2012 at 14:08
1
  • @Aliostad I edited the question. It holds a set of quote ID's Commented Mar 21, 2012 at 14:17

3 Answers 3

1

This is probably part of a one-to-many relationship. Say for example your ArrayList is storing strings, then create a separate table, where one of its fields in this string value. You can send the ArrayList as a param to your DALC class and loop through it there, adding one record at a time to the DB by calling a stored proceedure, etc.

answered Mar 21, 2012 at 14:12

Comments

0

You can use a serializer such as

var serializer = new XmlSerializer(typeof(ArrayList));
var buffer = new StringBuilder();
using(var tw = new XmlTextWriter(new StringWriter(buffer)){
 serializer.serialize(tw, myArrayList);
}
var xml = buffer.ToString();

BUT There are a few warnings:

answered Mar 21, 2012 at 14:15

Comments

0
Dim no As Integer() = {11, 22, 33, 44}
 Dim MS As New System.IO.MemoryStream
 Dim bin As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
 bin.Serialize(MS, no)
 Dim bytes(MS.Length - 1) As Byte
 MS.Position = 0
 MS.Read(bytes, 0, bytes.Length)
 ....
 ...
 cmd.ComandText="insert into TableName values (@data)"
 cmd.Parameters.Add("@data", SqlDbType.Image, bytes.Length).Value = bytes

Source: http://www.daniweb.com/software-development/vbnet/threads/253410/save-array-to-database

answered Mar 21, 2012 at 14:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.