http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html

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, 0, int.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();
        }
    }
}

Web Service 中返回DataSet结果大小改进的更多相关文章

  1. Web Service 中返回DataSet结果的几种方法

    Web Service 中返回DataSet结果的几种方法: 1)直接返回DataSet对象    特点:通常组件化的处理机制,不加任何修饰及处理:    优点:代码精减.易于处理,小数据量处理较快: ...

  2. 在Web Service中傳送Dictionary

    有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMethod] public Dictionary< ...

  3. 问题:不支持Dictionary;结果:在Web Service中傳送Dictionary

    在Web Service中傳送Dictionary 有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMe ...

  4. ASP.NET Web Service中使用Session 及 Session丢失解决方法 续

    原文:ASP.NET Web Service中使用Session 及 Session丢失解决方法 续 1.关于Session丢失问题的说明汇总,参考这里 2.在Web Servcie中使用Sessio ...

  5. 从WEB SERVICE 上返回大数据量的DATASET

    前段时间在做一个项目的时候,遇到了要通过WEB SERVICE从服务器上返回数据量比较大的DATASET,当然,除了显示在页面上以外,有可能还要用这些数据在客户端进行其它操作.查遍了网站的文章,问了一 ...

  6. 企业级SOA之路——在Web Service中使用HTTP和JMS

    原文:http://www.tibco.com/resources/solutions/soa/enterprise_class_soa_wp.pdf   概述     IT业界在早期有一种误解,认为 ...

  7. Web Api 中返回JSON的正确做法

    在使用Web Api的时候,有时候只想返回JSON:实现这一功能有多种方法,本文提供两种方式,一种传统的,一种作者认为是正确的方法. JSON in Web API – the formatter b ...

  8. Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)

    在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...

  9. 转-Web Service中三种发送接受协议SOAP、http get、http post

    原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...

随机推荐

  1. 虚拟机VMware与主机共享文件介绍

    我们经常会在Windows平台安装虚拟机VMware,不管是出于实验测试还是工作需要,伴随而来的就是经常需要在Windows系统和虚拟机系统之间进行共享数据文件,例如,需要将Window主机上的Ora ...

  2. android 发送短信功能

    private void sendSMS(String num,String smsBody) { String phoneNum = "smsto:" + num; Uri sm ...

  3. spf13-vim安装与使用

    一.简介 spf13-vim是Vim插件与配置的一个发行版本,包含了一整套精心挑选的Vim插件,采用Vundle进行插件管理,并且可以通过下列文件进行个性化配置 ~/.vimrc.local #个性化 ...

  4. 配置TortoiseSVN客户端, 强制签入前加注释

    正如上篇提到, 总有一些人在签入代码到SVN前没有加注释, 然后, 像这样: 鬼才知道改了什么东西. ①有些人可能就是没有写注释的习惯, ②有些人可能是忘记写注释 && SVN服务端和 ...

  5. 我也来谈一谈c++模板(一)

    c++中程序员使用模板能够写出与类型无关的代码,提高源代码重用,使用合适,大大提高了开发效率.此前,可以使用宏实现模板的功能,但是模板更加安全.清晰.在编写模板相关的代码是我们用到两个关键词:temp ...

  6. nginx 301 永久重定向

    nginx301跳转设置很简单,配置如下. (配置文件默认为nginx.conf,如果制定了新的配置文件,在新的文件配置即可.) server{ server_name xxx.com www.xxx ...

  7. 兼容可控硅调光的一款LED驱动电路记录

    1.该款电路为兼容可控硅调光的LED驱动电路,采用OB3332为开关控制IC,拓扑方案为Buck: 2.FB1:磁珠的单位是欧姆,而不是亨利,这一点要特别注意.因为磁珠的单位是按照它在某一频率 产生的 ...

  8. sudo 命令情景分析

    Linux 下使用 sudo 命令,可以让普通用户也能执行一些或者全部的 root 命令.本文就对我们常用到 sudo 操作情景进行简单分析,通过一些例子来了解 sudo 命令相关的技巧. 情景一:用 ...

  9. CNI插件源码示例,对于github.com/rajatchopra/ocicni库的分析

    CNI插件初始化 // ocicni.go 1.func InitCNI(pluginDir string) (CNIPlugin, error) (1).先调用plugin := probeNetw ...

  10. 电脑控制Android设备的软件——Total Control

    最早开始搞Android开发时,为了调试方便,想找一个Android下的远程控制软件,支持在电脑端远程控制和同步显示Android设备.先后试了360手机助手.Mobizen.Vysor和Mirror ...