发布: 2008-5-06 11:32 | 作者: 王翔 | 来源: 本站原创 | 查看: 77次 | 进入软件测试论坛讨论
1using System; 2using System.IO; 3using System.Data; 4using System.Runtime.Serialization; 5using System.Runtime.Serialization.Formatters.Binary; 6 7namespace Common 8{ 9 public class DataFormatter 10 { 11 private DataFormatter() { } 12 /**//// <summary> 13 /// Serialize the Data of dataSet to binary format 14 /// </summary> 15 /// <param name="dsOriginal"></param> 16 /// <returns></returns> 17 static public byte[] GetBinaryFormatData(DataSet dsOriginal) 18 { 19 byte[] binaryDataResult = null; 20 MemoryStream memStream = new MemoryStream(); 21 IFormatter brFormatter = new BinaryFormatter(); 22 dsOriginal.RemotingFormat = SerializationFormat.Binary; 23 24 brFormatter.Serialize(memStream, dsOriginal); 25 binaryDataResult = memStream.ToArray(); 26 memStream.Close(); 27 memStream.Dispose(); 28 return binaryDataResult; 29 } 30 /**//// <summary> 31 /// Retrieve dataSet from data of binary format 32 /// </summary> 33 /// <param name="binaryData"></param> 34 /// <returns></returns> 35 static public DataSet RetrieveDataSet(byte[] binaryData) 36 { 37 DataSet dataSetResult = null; 38 MemoryStream memStream = new MemoryStream(binaryData); 39 IFormatter brFormatter = new BinaryFormatter(); 40 41 object obj = brFormatter.Deserialize(memStream); 42 dataSetResult = (DataSet)obj; 43 return dataSetResult; 44 } 45 } 46} 47
文章来源于领测软件测试网 https://www.ltesting.net/