1. directly return the DataSet object
Feature: the DataSet object is returned directly.
Application scenarios: 1. intranet. 2. When the Internet and the data size are kb.
2. Return the byte array after the DataSet object is serialized using binary.
Features: The processing mode of byte array streams.
Application Scenario: big data exchange.
3. Return the byte array after the datasetsurrogate object is serialized using binary.
Features: serialization using open-source components provided by Microsoft is still the processing mode of byte streams. For more information, see: http://support.microsoft.com/kb/829740/zh-cn
Application Scenario: big data exchange.
4. Return the byte array after the datasetsurrogate object is serialized and zip in binary format.
Features: the open-source component provided by Microsoft is used to compress and transmit byte stream arrays, which is still the processing mode of byte streams. For more information, see: http://support.microsoft.com/kb/829740/zh-cn
Application Scenario: This method is recommended when the Internet environment needs to transmit large data volumes of network data. This is also a method that I strongly recommend to you.
The WebServices code is as follows:
- WebServices
- Using system;
- Using system. Collections. Generic;
- Using system. Web;
- Using system. Web. Services;
- Using system. Data;
- Using Microsoft. Practices. enterpriselibrary. Common;
- Using Microsoft. Practices. enterpriselibrary. Data;
- Using system. IO;
- Using system. Io. compression;
- Using system. runtime. serialization. formatters. Binary;
- Namespace webservice1
- {
- /// <Summary>
- /// Summary of service1
- /// </Summary>
- [WebService (namespace = "http://tempuri.org/")]
- [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
- [System. componentmodel. toolboxitem (false)]
- Public class service1: system. Web. Services. WebService
- {
- [Webmethod (description = "datasdatasdatas datasdatasdatas")]
- Public dataset getdataset ()
- {
- String SQL = "select * from MERs ";
- Database DB = databasefactory. createdatabase ();
- Dataset DS = dB. executedataset (commandtype. Text, SQL );
- Return Ds;
- }
- [Webmethod (description = "Returned DataSet object byte array after binary serialization")]
- Public byte [] getbytes ()
- {
- Dataset DS = getdataset ();
- Binaryformatter BF = new binaryformatter ();
- Memorystream MS = new memorystream ();
- BF. serialize (MS, DS );
- Byte [] buffer = Ms. toarray ();
- Return buffer;
- }
- [Webmethod (description = "returns the byte array of the datasetsurrogate object serialized in binary")]
- Public byte [] getdatasetsurrogatebytes ()
- {
- Dataset DS = getdataset ();
- Datasetsurrogate DSS = new datasetsurrogate (DS );
- Binaryformatter BF = new binaryformatter ();
- Memorystream MS = new memorystream ();
- BF. serialize (MS, DSS );
- Byte [] buffer = Ms. toarray ();
- Return buffer;
- }
- [Webmethod (description = "returns the datasetsurrogate object serialized in binary and zip compressed byte array")]
- Public byte [] getdatasetsurrogatezipbytes ()
- {
- Dataset DS = getdataset ();
- Datasetsurrogate DSS = new datasetsurrogate (DS );
- Binaryformatter BF = new binaryformatter ();
- Memorystream MS = new memorystream ();
- BF. serialize (MS, DSS );
- Byte [] buffer = Ms. toarray ();
- Byte [] zipbuffer = compress (buffer );
- Return zipbuffer;
- }
- // Compressed byte array
- Public byte [] compress (byte [] data)
- {
- Memorystream MS = new memorystream ();
- Stream zipstream = new gzipstream (MS, compressionmode. Compress, true );
- Zipstream. Write (data, 0, Data. Length );
- Zipstream. Close ();
- Ms. Position = 0;
- Byte [] buffer = new byte [Ms. Length];
- Ms. Read (buffer, 0, Int. parse (Ms. length. tostring ()));
- Return buffer;
- }
- }
- }
Copy code
The code for the client to call WebServices is as follows:
The client calls WebServices.
- Using system;
- Using system. Collections. Generic;
- Using system. Web;
- Using system. Web. UI;
- Using system. Web. UI. webcontrols;
- Using webservicesclient. localhost;
- Using system. Data;
- Using system. runtime. serialization. formatters. Binary;
- Using system. IO;
- Using system. diagnostics;
- Namespace webservicesclient
- {
- Public partial class _ default: system. Web. UI. Page
- {
- Service1 S = new service1 ();
- Protected void page_load (Object sender, eventargs E)
- {
- }
- // Directly return the DataSet object
- Protected void button#click (Object sender, eventargs E)
- {
- Stopwatch Sw = new stopwatch ();
- Sw. Start ();
- Dataset DS = S. getdataset ();
- Gridview1.datasource = Ds. Tables [0]. defaultview;
- Gridview1.databind ();
- Sw. Stop ();
- Label1.text = string. Format ("Time consumed: {0} millisecond", SW. elapsedmilliseconds. tostring ());
- }
- // Obtain the byte array after the DataSet object is serialized using binary.
- Protected void button2_click (Object sender, eventargs E)
- {
- Stopwatch Sw = new stopwatch ();
- Sw. Start ();
- Byte [] buffer = S. getbytes ();
- Binaryformatter BF = new binaryformatter ();
- Dataset DS = BF. deserialize (New memorystream (buffer) as dataset;
- Gridview1.datasource = Ds. Tables [0]. defaultview;
- Gridview1.databind ();
- Sw. Stop ();
- Label2.text = string. Format ("Time consumed: {1} millisecond; data size: {0}", buffer. length. tostring (), SW. elapsedmilliseconds. tostring ());
- }
- // Obtain the byte array after the datasetsurrogate object is serialized using binary.
- Protected void button3_click (Object sender, eventargs E)
- {
- Stopwatch Sw = new stopwatch ();
- Sw. Start ();
- Byte [] buffer = S. getdatasetsurrogatebytes ();
- Binaryformatter BF = new binaryformatter ();
- Datasetsurrogate DSS = BF. deserialize (New memorystream (buffer) as datasetsurrogate;
- Dataset DS = DSS. converttodataset ();
- Gridview1.datasource = Ds. Tables [0]. defaultview;
- Gridview1.databind ();
- Sw. Stop ();
- Label3.text = string. Format ("Time consumed: {1} millisecond; data size: {0}", buffer. length. tostring (), SW. elapsedmilliseconds. tostring ());
- }
- // Obtain the byte array after the datasetsurrogate object is serialized and zip by binary.
- Protected void button4_click (Object sender, eventargs E)
- {
- Stopwatch Sw = new stopwatch ();
- Sw. Start ();
- Byte [] zipbuffer = S. getdatasetsurrogatezipbytes ();
- Byte [] buffer = unzip. Decompress (zipbuffer );
- Binaryformatter BF = new binaryformatter ();
- Datasetsurrogate DSS = BF. deserialize (New memorystream (buffer) as datasetsurrogate;
- Dataset DS = DSS. converttodataset ();
- Gridview1.datasource = Ds. Tables [0]. defaultview;
- Gridview1.databind ();
- Sw. Stop ();
- Label4.text = string. Format ("Time consumed: {1} millisecond; data size: {0}", zipbuffer. length. tostring (), SW. elapsedmilliseconds. tostring ());
- }
- }
- }
Copy code
The test results are shown in sequence:
2009-4-13.jpg(33.94 K) 12:21:12
Special instructions on the test results: because the test environment is local and the data volume is not large, the test results are not very close to the actual situation. If you have any conditions, you can test them, we also hope to provide the test results for your reference. (Wen/Shen Shan laolin)
The development environment is vs2008 SP1 + sqlserver2008sp1. The database is the northwind database.
Original article: http://www.pin5i.com/showtopic-23733.html