Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving multidimensional byte array to SQL Server database

I want to save a multidimensional byte array to a SQL Server database.

I know how to save a byte array which is a image conversion to a database. For that the data type I used is image. But now I want to store another byte array which is multidimensional byte array byte [,] temp, which has two dimensions with x,y values.

I searched on internet and here, it is said that using VARBINARY format. All I want to know is if I save my multidimensional array in a VARBINARY datatype data column, will the values will be altered? Is it possible to receive the data back as multidimensional array again?

like image 380
Gihan Avatar asked Jul 04 '12 18:07

Gihan


People also ask

Can you store a byte array in SQL?

To save byte array into sql server, equivalent data type is varbinary in SQL Server. You can almost save any kind of data using varbinary datatype and even though we have image datatype, varbinary is the recommended datatype to store image in sql server instead of image datatype.

How do you store multidimensional arrays?

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements. Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.

How are multidimensional arrays stored in memory?

The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order.


2 Answers

Yes, you will be able to get back your multi-dimensional array unaltered.

How can you do it? Using a Varbinary(max) field in Sql Server and saving into it the serialized multidimensional byte array. In order to get your array back, obviusly, you need to deserialize what you stored in the database.

Here is an example of how to do it:

public void TestSO()
{
    using (SqlConnection conexion = new SqlConnection())
    {
        using (SqlCommand command = new SqlCommand())
        {
            //This is the original multidimensional byte array
            byte[,] byteArray = new byte[2, 2] {{1, 0}, {0,1}};
            ConnectionStringSettings conString = ConfigurationManager.ConnectionStrings["ConnectionString"];
            conexion.ConnectionString = conString.ConnectionString;
            conexion.Open();
            command.Connection = conexion;
            command.CommandType = CommandType.Text;
            command.CommandText = "UPDATE Table SET VarBinaryField = @Content WHERE Id = 73 ";
            command.Parameters.Add(new SqlParameter("@Content", SqlDbType.VarBinary, -1));
            //Serialize the multidimensional byte array to a byte[]
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, byteArray);
            //Set the serialized original array as the parameter value for the query
            command.Parameters["@Content"].Value = ms.ToArray();
            if (command.ExecuteNonQuery() > 0)
            {
                //This method returns the VarBinaryField from the database (what we just saved)
                byte[] content = GetAttachmentContentsById(73);
                //Deserialize Content to a multidimensional array
                MemoryStream ms2 = new MemoryStream(content);
                byte[,] fetchedByteArray = (byte[,])bf.Deserialize(ms2);
                //At this point, fetchedByteArray is exactly the same as the original byte array
            }
        }
    }
}
like image 174
daniloquio Avatar answered Oct 01 '22 23:10

daniloquio


As I know there is no appropriate data type in Microsoft SQL Server to store multidimensional arrays. However there are many ways to save information about array structure. Some of them:

  1. create several columns of BINARY (fixed-length) data type and each row of your multidimensional array to appropriate column; in this case it is expected that count of rows in your array is constant;

  2. store the whole array as one-dimensional array into single column of VARBINARY (variable-length) data type and store in separate column of INT data type the count of elements in each row of multidimensional array; in this case it is expected that count of elements in each row is the same (not jagged C# array); when reading array then you will be able to split elements by this length into separate rows of multidimensional array.

like image 32
Ivan Avatar answered Oct 02 '22 00:10

Ivan