Web Service 中返回DataSet結果大小改進


Web Service 中返回DataSet結果方法:

1)直接返回DataSet對象
    特點:通常組件化的處理機制,不加任何修飾及處理;
    優點:代碼精減、易於處理,小數據量處理較快;
    缺點:大數據量的傳遞處理慢,消耗網絡資源;
    建議:當應用系統在內網、專網(局域網)的應用時,或外網(廣域網)且數據量在KB級時的應用時,采用此種模式。

 

2)返回DataSet對象用Binary序列化后的字節數組
    特點:字節數組流的處理模式;
    優點:易於處理,可以中文內容起到加密作用;
    缺點:大數據量的傳遞處理慢,較消耗網絡資源;
    建議:當系統需要進行較大數據交換時采用。

 

3)返回DataSetSurrogate對象用Binary 序列化后的字節數組
    特點:微軟提供的開源組件;下載地址: http://support.microsoft.com/kb/829740/zh-cn
    優點:易於處理,可以中文內容起到加密作用;
    缺點:大數據量的傳遞處理慢,較消耗網絡資源;
    建議:當系統需要進行較大數據交換時采用。

 

4)返回DataSetSurrogate對象用Binary 序列化並Zip壓縮后的字節數組
    特點:對字節流數組進行壓縮后傳遞;
    優點:當數據量大時,性能提高效果明顯,壓縮比例大;
    缺點:相比第三方組件,壓縮比例還有待提高;
    建議:當系統需要進行大數據量網絡數據傳遞時,建議采用此種可靠、高效、免費的方法。

 

WebService代碼:

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 = "直接返回DataSet對象")]
         public DataSet GetDataSet()
        {
            DataSet DS =  new DataSet();
             string sql =  " SELECT * FROM [Customers] ";
             using (SqlConnection conn =  new SqlConnection(connectionString))
            {
                conn.Open();
                SqlDataAdapter dataAd =  new SqlDataAdapter(sql, conn);
               
                dataAd.Fill(DS);
            }
             return DS;
        }

        [WebMethod(Description = "返回DataSet對象用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 = "返回DataSetSurrogate對象用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 = "返回DataSetSurrogate對象用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>
        
///  壓縮二進制數據
        
///   </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,  0int.Parse(ms.Length.ToString()));
             return compressedData;
        }
    }
}

 

調用代碼:

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;

namespace Test
{
     public  partial  class _Default : System.Web.UI.Page
    {
         protected  void Page_Load( object sender, EventArgs e)
        {

        }
        WService.DataSetServiceSoapClient _Ds =  new WService.DataSetServiceSoapClient();

         private  void BindDataSet(DataSet DS)
        {
             this.GridView1.DataSource = DS.Tables[ 0];
             this.GridView1.DataBind();
        }

         ///   <summary>
        
/// 調用 直接返回DataSet對象
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         protected  void Button1_Click( object sender, EventArgs e)
        {
           DataSet ds= _Ds.GetDataSet();
           BindDataSet(ds);
        }

         ///   <summary>
        
///  調用  返回DataSet對象用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>
        
///  調用  返回DataSetSurrogate對象用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>
        
///  調用  返回DataSetSurrogate對象用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();
        }
    }
}

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM