.net 2.0远程传输数据集的优化方法(续)
对数据集序列化和反序列化的方法进行了一下简单的封装,使其可以得到重用的效果.见下面的类DatFormatter.
通过GetBinaryFormatData方法可以转换数据集为二进制,在服务器端使用,转换数据集格式。发送,客户端接收,得到二进制格式数据,使用RetrieveDataSet方法,反序列化,得到数据集,进行客户端操作。通过这些简单的操作(序列化和反序列化,将数据压缩),可以使数据集等体积庞大的对象在远程传递中的时间大大减少,并且可以减少网络中断等问题对程序的影响。
通过GetBinaryFormatData方法可以转换数据集为二进制,在服务器端使用,转换数据集格式。发送,客户端接收,得到二进制格式数据,使用RetrieveDataSet方法,反序列化,得到数据集,进行客户端操作。通过这些简单的操作(序列化和反序列化,将数据压缩),可以使数据集等体积庞大的对象在远程传递中的时间大大减少,并且可以减少网络中断等问题对程序的影响。
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
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