Method for returning dataset results in Web Service:
1) directly return the DataSet object
Features: Usually componentized processing mechanism without any modification or processing;
Advantages:CodeReduced, easy to process, and fast processing of small data volumes;
Disadvantages: Transmission of large data volumes is slow and network resources are consumed;
Suggestion: This mode is used when the application system is used for Intranet, private network (LAN) applications, or Internet (WAN) applications with a data size of kb.
2) returns the byte array after the DataSet object is serialized using binary.
Features: processing mode of byte array streams;
Advantage: it is easy to process and can encrypt Chinese content;
Disadvantages: Transmission of large data volumes is slow and network resources are consumed;
Suggestion: It is used when the system needs to exchange large data.
3) returns the byte array after the datasetsurrogate object is serialized using binary.
Features: open source components provided by Microsoft;: http://support.microsoft.com/kb/829740/zh-cn
Advantage: it is easy to process and can encrypt Chinese content;
Disadvantages: Transmission of large data volumes is slow and network resources are consumed;
Suggestion: It is used when the system needs to exchange large data.
4) returns the byte array after the datasetsurrogate object is serialized and zip in binary format.
Features: compress the byte stream array before passing it;
Advantage: when the data volume is large, the performance improvement is obvious and the compression ratio is large;
Disadvantages: the compression ratio remains to be improved compared with third-party components;
Suggestion: This reliable, efficient, and free method is recommended when the system needs to transmit big data network data.
WebService code:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. Services;
Using System. Data;
Using System. Data. sqlclient;
Using System. runtime. serialization;
Using System. runtime. serialization. formatters. Binary;
Using System. IO;
Using System. Io. compression;
Namespace Datasetwebservice
{
/// <Summary>
/// Summary Description for service1
/// </Summary>
[WebService (namespace = " Http://tempuri.org/ " )]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[System. componentmodel. toolboxitem ( False )]
Public Class Datasetservice: system. Web. Services. WebService
{
String Connectionstring = " Server =.; database = northwind; user id = sa; Password = sa; " ;
[Webmethod (description ="Returns the DataSet object directly.")]
Public Dataset getdataset ()
{
Dataset DS = New Dataset ();
String SQL = " Select * from [MERs] " ;
Using (Sqlconnection conn = New Sqlconnection (connectionstring ))
{
Conn. open ();
Sqldataadapter dataad = New Sqldataadapter (SQL, Conn );
Dataad. Fill (DS );
}
Return DS;
}
[Webmethod (description ="Returns the byte array after the DataSet object is serialized in binary.")]
Public Byte [] Getdatasetbytes ()
{
Dataset DS = getdataset ();
Binaryformatter SER =New Binaryformatter ();
Memorystream MS = New Memorystream ();
Ser. 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 SER = New Binaryformatter ();
Memorystream MS = New Memorystream ();
Ser. serialize (MS, DSS );
Byte [] Buffer = Ms. toarray ();
Return Buffer;
}
[Webmethod (description ="Returns the byte array of the datasetsurrogate object serialized in binary.")]
Public Byte [] Getdatasetsurrogatezipbytes ()
{
Dataset DS = getdataset ();
Datasetsurrogate DSS = New Datasetsurrogate (DS );
Binaryformatter SER = New Binaryformatter ();
Memorystream MS = New Memorystream ();
Ser. serialize (MS, DSS );
Byte [] Buffer = Ms. toarray ();
Byte [] Zipbuffer = compress (buffer );
Return Zipbuffer;
}
/// <Summary>
/// Compressing binary data
/// </Summary>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public Byte [] Compress ( Byte [] Data)
{
Memorystream MS = New Memorystream ();
Stream zipstream =Null ;
Using (Zipstream = New Gzipstream (MS, compressionmode. Compress, True ))
{
Zipstream. Write (data, 0 , Data. Length );
Zipstream. Close ();
}
Ms. Position = 0 ;
Byte [] Compresseddata = New Byte [Ms. Length];
Ms. Read (compresseddata,0 , Int . Parse (Ms. length. tostring ()));
Return Compresseddata;
}
}
}
Call code:
Using System;
Using System. Collections. Generic;
Using System. Data;
Using System. LINQ;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. IO;
Using System. Io. compression;
Using System. runtime. serialization. formatters. Binary;
NamespaceTest
{
Public Partial Class_ Default: system. Web. UI. Page
{
Protected VoidPage_load (ObjectSender, eventargs E)
{
}
Wservice. datasetservicesoapclient _ DS =NewWservice. datasetservicesoapclient ();
Private VoidBinddataset (Dataset DS)
{
This. Gridview1.datasource = Ds. Tables [0];
This. Gridview1.databind ();
}
/// <Summary>
/// The DataSet object is returned directly by calling.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected Void Button#click ( Object Sender, eventargs E)
{
Dataset DS = _ DS. getdataset ();
Binddataset (DS );
}
/// <Summary>
/// Return the byte array after the DataSet object is serialized in binary.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected Void Button2_click ( Object Sender, eventargs E)
{
Byte [] Buffer = _ DS. getdatasetbytes ();
Binaryformatter BF = New Binaryformatter ();
Dataset DS = BF. deserialize ( New Memorystream (buffer )) As Dataset;
Binddataset (DS );
Label2.text = buffer. length. tostring ();
}
/// <Summary>
/// Return the byte array after the datasetsurrogate object is serialized using binary.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected Void Button3_click ( Object Sender, eventargs E)
{
Byte [] Buffer = _ DS. getdatasetsurrogatebytes ();
Binaryformatter BF = New Binaryformatter ();
Datasetsurrogate DSS = BF. deserialize ( New Memorystream (buffer )) As Datasetsurrogate;
Dataset DS = DSS. converttodataset ();
Binddataset (DS );
Label3.text = buffer. length. tostring ();
}
/// <Summary>
/// Return the byte array after the datasetsurrogate object is serialized using binary.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected Void Button4_click ( Object Sender, eventargs E)
{
Byte [] Buffer = _ DS. getdatasetsurrogatezipbytes ();
Memorystream MS = New Memorystream (buffer );
Stream zipstream = New Gzipstream (MS, compressionmode. Decompress );
Byte [] Dc_data = Null ;
Int Totalbytesread = 0 ;
While ( True )
{
Array. Resize ( Ref Dc_data, totalbytesread + buffer. Length +1 );
Int Bytesread = zipstream. Read (dc_data, totalbytesread, buffer. Length );
If (Bytesread = 0 )
{
Break ;
}
Totalbytesread + = bytesread;
}
Binaryformatter BF = New Binaryformatter ();
Datasetsurrogate DSS = BF. deserialize ( New Memorystream (dc_data ))As Datasetsurrogate;
Dataset DS = DSS. converttodataset ();
Binddataset (DS );
Label4.text = buffer. length. tostring ();
}
}
}