In vs2005, 12000 records are queried and remotingformat = serializationformat. Binary is set;
Re-serialization, transmitted through WebService, received by the client, and deserialized, is indeed much better than the direct transmission of dataset, not only in network transmission, even the local machine, performance improvement is also very obvious.
The following are the methods in WebService and the client deserialization Method for dataset.
1. Take the data on the server, fill the dataset, and convert it to the binary format./** // <summary>
/// Method for users data query with binaryformatter
/// </Summary>
/// <Param name = "Err"> </param>
/// <Returns> </returns>
Public byte [] binaryuserselect (ref string ERR)
{
Clearcommand ();
M_commandstringbuilder.append ("select * From t_users ;");
Dataset dsresult = new dataset ();
Byte [] barrayresult = NULL;
Try
{
Dsresult = sqlhelper. executedataset (m_currentconnectionstring, commandtype. Text, m_commandstringbuilder.tostring ());
// The above data is obtained, so you don't need to worry about it. The binary compressed dataset is the following small segment.
Dsresult. remotingformat = serializationformat. Binary;
Memorystream MS = new memorystream ();
Iformatter BF = new binaryformatter ();
BF. serialize (MS, dsresult );
Barrayresult = Ms. toarray ();
Ms. Close ();
//
}
Catch (exception ee)
{
Err = ee. tostring ();
}
Return barrayresult;
}
2. Send data in byte [] format to the client through WebService. This is what WebService is doing. We don't need to worry about it.
3. The client receives data in byte [] format, deserializes it, obtains the dataset, and performs client operations.
/** // <Summary>
/// Get user data with binary format
/// </Summary>
/// <Returns> </returns>
Public dataset getbinaryuserdata ()
{
String err = "";
Byte [] buserdata = SVC. bytearrayuserselect (ref ERR );
If (Err! = "")
{
MessageBox. Show (ERR );
Err = "";
Return NULL;
}
// Deserialization Process
Memorystream MS = new memorystream (buserdata );
Iformatter BF = new binaryformatter ();
Object OBJ = BF. deserialize (MS );
Dataset dsresult = (Dataset) OBJ;
//
Ms. Close ();
Return dsresult;
}
Same
Sample one machine, which generates 12000 pieces of data manually, reads and transfers data using WebService locally, and displays data in the client dataset and byte [] format. The average time of the former is
2.3 seconds. The average time of the latter is 1.7 seconds. The difference is only in the format of the transmission process, and the serialization and deserialization time required by the latter. The difference in local WebService transmission is still as follows:
In this case, the network transmission time optimization will naturally become more obvious ..
. Net1.1 the datasetsurrogate Development Kit provided by Microsoft below: http://support.microsoft.com/default.aspx? SCID = KB; en-US; 829740 datasetsurrogate comes with. NET 2.0
In. the implementation method in net1.1 is as follows. There are two methods: You can save the serialized data in the client hard disk as a file; you can also use the byte [] method to return the data to the client, the following is the code.
Web service end (file form)
[Webmethod (description = "obtain remote dataset cyclically")]
Public void surrogatereadtable (string tablename)
{
// Serialize dataset to binary stream through surrogate class
Dataset Ds;
DS = sqlhelper. executedataset (CNN, commandtype. Text, "select * from" + tablename );
// Instantiate datasetsurrogate and upload the retrieved dataset to the constructor.
SDS = new datasetsurrogate (DS );
// Instantiate the binary stream
Binaryformatter BF = new binaryformatter ();
Streamwriter swdat;
// Write to a local file
Swdat = new streamwriter (@ "C: \ output_surrogate_dataset.dat ");
BF. serialize (swdat. basestream, SDS );
// The size of the serialized File
Long size = swdat. basestream. length;
Swdat. Close ();
}
Client
Private void button#click (Object sender, system. eventargs E)
{
Label1.text = datetime. Now. tostring ();
Button1.enabled = false;
// Deserialization binary stream can be converted to dataset through surrogate class
// Read method from Web Service
SVS. surrogateread ("t_busdocbase ");
Binaryformatter BF = new binaryformatter ();
Streamreader swdat;
Swdat = new streamreader (@ "C: \ output_surrogate_dataset.dat ");
Object o = BF. deserialize (swdat. basestream );
Dataset Ds;
SDS = (datasetsurrogate) O;
DS = SDS. converttodataset ();
Datagrid1.datasource = Ds. Tables [0];
Swdat. Close ();
}
Web service end (byte [] mode)
[Webmethod (description = "getting business data remote dataset")]
Public byte [] surrogateread1 ()
{
Dataset Ds;
DS = sqlhelper. executedataset (CNN, commandtype. Text, "select * From t_busdocbase ");
SDS = new datasetsurrogate (DS );
Memorystream S = new memorystream ();
Binaryformatter BF = new binaryformatter ();
BF. serialize (S, SDS );
Byte [] e = S. toarray ();
Return E;
}
Client
Private void button3_click (Object sender, system. eventargs E)
{
Label1.text = datetime. Now. tostring ();
Button3.enabled = false;
// * Deserializing binary stream can be converted to dataset through surrogate class */
// Read method from Web Service
Byte [] BB = SVS. surrogateread1 ();
Memorystream BR = new memorystream (bb );
Binaryformatter BF = new binaryformatter ();
Object o = BF. deserialize (BR );
SDS = (datasetsurrogate) O;
DS = SDS. converttodataset ();
Datagrid1.datasource = Ds. Tables [0];
BR. Close ();
}
I personally think byte [] is safer. After all, you don't have to generate files on the client, and you don't have to worry about data security.
In 2.0, we made a simple encapsulation of the serialization and deserialization methods of datasets so that they can be reused. For details, see the following class datformatter.
Connect
By using the getbinaryformatdata method, you can convert a dataset to a binary value, which is used on the server side to convert the dataset format. Send and receive data in binary format by the client.
Retrievedataset method, deserialize, obtain the dataset, and perform client operations. Through these simple operations (serialization and deserialization, data compression), you can make datasets and other volumes
The time required for a large object to be remotely transmitted is greatly reduced, and network interruption and other problems can be reduced.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