/// <summary>
        ///     Gets data from the specified DataTable and serializes it into the outgoing data stream
        /// </summary>
        /// <param name = "outgoingData">The outgoing data.</param>
        /// <param name = "target">The target.</param>
        /// <remarks>
        ///     The default binary serialization of a DataTable is XML but we want the data to be serialized binary "column" wise
        ///     so a DataTableSurrogate is used to do this.
        /// </remarks>
        public static void Serialize(Stream outgoingData, DataTable target)
        {
            outgoingData.Position = 0;
            var dataTableSurrogate = new DataTableSurrogate(target);

            Serialize(outgoingData, dataTableSurrogate);
        }
        private static void TestDataTableSurrogateSerializeDeserialize(DataTable datatable)
        {
            var target          = new DataTableSurrogate(datatable);
            var memoryStream    = new MemoryStream();
            var binaryFormatter = new BinaryFormatter();

            binaryFormatter.Serialize(memoryStream, target);
            memoryStream.Position = 0;
            var result = binaryFormatter.Deserialize(memoryStream) as DataTableSurrogate;

            Assert.IsNotNull(result);
            var resultDataTable = result.ConvertToDataTable();

            Assert.AreEqual(datatable.Columns.Count, resultDataTable.Columns.Count);
            Assert.AreEqual(datatable.Rows.Count, resultDataTable.Rows.Count);
            //CollectionAssert.AreEqual(datatable.DefaultView, resultDataTable.DefaultView);
        }
示例#3
0
        public static byte[] SerializeBinary(object[] obj)
        {
            if (obj == null || obj.Length == 0)
                return new byte[] { };

            for (int i = 0; i < obj.Length; i++)
            {
                if (obj[i] != null)
                {
                    if (obj[i] is DataSet)
                        obj[i] = new DataSetSurrogate((DataSet)obj[i]);
                    else if (obj[i] is DataTable)
                        obj[i] = new DataTableSurrogate((DataTable)obj[i]);
                }
            }
            BinaryFormatter se = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream();
            se.Serialize(memStream, obj);
            byte[] bobj = memStream.ToArray();
            memStream.Close();
            return bobj;
        }
示例#4
0
文件: DataFormat.cs 项目: eleooo/App
 /// <summary>
 /// 将DataTable格式化成字节数组byte[]
 /// </summary>
 /// <param name="dt">DataTable对象</param>
 /// <returns>字节数组</returns>
 public static byte[] GetBinFormatData(DataTable dt)
 {
     if (dt == null)
         return null;
     byte[] binaryDataResult = null;
     using (MemoryStream memStream = new MemoryStream( ))
     {
         DataTableSurrogate dss = new DataTableSurrogate(dt);
         IFormatter brFormatter = new BinaryFormatter( );
         brFormatter.Serialize(memStream, dss);
         binaryDataResult = memStream.ToArray( );
     }
     return binaryDataResult;
 }