Optimization Method for. NET 2.0 Remote Data Set transmission (continued)

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.