反序列化自定义对象时,二进制流“0”不包含有效的BinaryHeader错误

时间:2013-06-17 12:36:27

标签: c# serialization

在反序列化自定义对象时遇到此错误 我试图将自定义类的集合插入到sql数据库&检索它插入顺利但检索数据&反序列化给我这个错误 我的代码示例:

        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
        }
    }

2 个答案:

答案 0 :(得分:2)

我在自定义类的序列化和反序列化中遇到了同样的问题。我环顾四周,他们有相同的代码标记为解决方案(因为您的代码显示在顶部),但我无法使其正确运行。我在memoryStream.Write()方法之后收到的是一个零数组。我改变了我的代码并让它发挥作用。

我所做的是(在您的代码中实现):

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

这是用于将字节数组发送到数据库。为了检索它,我做了以下事情:

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

试试吧!它真的对我有用。

答案 1 :(得分:1)

首先检查实际存储在该列中的内容,并检查列的类型是varbinary还是类似。该错误表明序列化对象的数据流严重损坏或被截断。如果行/列不包含长“hexstring”,则插入/更新会出现写入问题,并在那里进一步搜索。

相关问题