The methods of dataset serialization and deserialization are simply encapsulated so that they can be reused. For details, see the following DatFormatter class.
You can use the GetBinaryFormatData method to convert a data set to binary. You can use this method on the server to convert the data set format. Send and receive data in binary format from the client. Use the RetrieveDataSet method to deserialize the data set and perform client operations. Through these simple operations (serialization and deserialization, data compression), you can greatly reduce the time required for the remote transmission of large objects such as datasets, in addition, it can reduce the impact of network interruptions and other problems on the program.
1 using System;
2 using System. IO;
3 using System. Data;
4 using System. Runtime. Serialization;
5 using System. Runtime. Serialization. Formatters. Binary;
6
7 namespace 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