通过GetBinaryFormatData方法可以转换数据集为二进制,在服务器端使用,转换数据集格式。发送,客户端接收,得到二进制格式数据,使用RetrieveDataSet方法,反序列化,得到数据集,进行客户端操作。通过这些简单的操作(序列化和反序列化,将数据压缩),可以使数据集等体积庞大的对象在远程传递中的时间大大减少,并且可以减少网络中断等问题对程序的影响。
1
using System;
2
using System.IO;
3
using System.Data;
4
using System.Runtime.Serialization;
5
using System.Runtime.Serialization.Formatters.Binary;
6![](/Images/OutliningIndicators/None.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
41
object obj = brFormatter.Deserialize(memStream);
42
dataSetResult = (DataSet)obj;
43
return dataSetResult;
44
}
45
}
46
}
47![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
47
![](/Images/OutliningIndicators/None.gif)