1

I faced this error while deserialization a custom object I am trying to insert a collection of custom class into sql database & retrieve it the insertion going well but retrieving the data & deserialize give me this error My code sample:

        private void InsertObject()
    {
        ReceiptCollection items = SqlDataRepository.ReceiptProvider.GetAll();

        string connectionString = "my connection";

        System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);

        string sql = "INSERT INTO [dbo].[LogHeader]([MasterObject]) VALUES (@MasterObject)";

        BinaryFormatter binaryFormatter = new BinaryFormatter();
        MemoryStream memoryStream = new MemoryStream();
        binaryFormatter.Serialize(memoryStream, items);

        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection))
        {
            byte[] bytes = new byte[memoryStream.Length];
            memoryStream.Write(bytes, 0, bytes.Length);

            connection.Open();
            cmd.Parameters.AddWithValue("@MasterObject", bytes);
            cmd.ExecuteNonQuery();
        }
    }

    private void RetrieveObjects()
    {
        string connectionString = "my connection";
        System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);

        string sql = "Select MasterObject From [dbo].[LogHeader] WHERE LogHeaderID=2";

        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection))
        {
            connection.Open();
            byte[] bytes = (byte[])cmd.ExecuteScalar();

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            MemoryStream memoryStream = new MemoryStream(bytes);
            memoryStream.Position = 0;
            ReceiptCollection items = (ReceiptCollection)binaryFormatter.Deserialize(memoryStream); // the error happened here
        }
    }
4

2 回答 2

2

I was facing the same problem in the serialization and deserialization of a custom class. Everywhere I looked around, they have the same code marked as the solution (as your code presented on the top) but I couldn't make it run correctly. All I was recieving after the memoryStream.Write() method, was an array of zeroes. I changed my code and got it to work.

What I did was (implemented in your code):

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    MemoryStream memoryStream = new MemoryStream();
    binaryFormatter.Serialize(memoryStream, items);

    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection))
    {
        byte[] bytes = new byte[memoryStream.Capacity];
        bytes = memoryStream.GetBuffer();

        connection.Open();
        cmd.Parameters.AddWithValue("@MasterObject", bytes);
        cmd.ExecuteNonQuery();
    }

This is for sending a byte array to the data base. For retrieving it, I did the following:

    connection.Open();
    byte[] bytes = (byte[])cmd.ExecuteScalar();

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream(bytes))
    {
        memoryStream.Position = 0;
        ReceiptCollection items = (ReceiptCollection)binaryFormatter.Deserialize(memoryStream);
    }

Try it! It really worked for me.

于 2013-07-16T17:10:10.113 回答
1

Start by checking what actually got stored in that column, and also check if the column's type is varbinary or similar. The error suggests that the serialized object's data stream got badly corrupted or truncated. If the row/column does not contain a long "hexstring", then there's a write problem with inserting/updating and search further there.

于 2013-06-17T12:38:40.873 回答