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

1)直接返回DataSet对象
    特点:通常组件化的处理机制,不加任何修饰及处理;
    优点:代码精减、易于处理,小数据量处理较快;
    缺点:大数据量的传递处理慢,消耗网络资源;
    建议:当应用系统在内网、专网(局域网)的应用时,或外网(广域网)且数据量在KB级时的应用时,采用此种模式。

2)返回DataSet对象用Binary序列化后的字节数组
    特点:字节数组流的处理模式;
    优点:易于处理,可以中文内容起到加密作用;
    缺点:大数据量的传递处理慢,较消耗网络资源;
    建议:当系统需要进行较大数据交换时采用。

3)返回DataSetSurrogate对象用Binary序列化后的字节数组
    特点:微软提供的开源组件;下载地址: http://support.microsoft.com/kb/829740/zh-cn
    优点:易于处理,可以中文内容起到加密作用;
    缺点:大数据量的传递处理慢,较消耗网络资源;
    建议:当系统需要进行较大数据交换时采用。

4)返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
    特点:对字节流数组进行压缩后传递;
    优点:当数据量大时,性能提高效果明显,压缩比例大;
    缺点:相比第三方组件,压缩比例还有待提高;
    建议:当系统需要进行大数据量网络数据传递时,建议采用此种可靠、高效、免费的方法。

代码示例:

服务端

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, , data.Length);
zipStream.Close();
}
ms.Position = ;
byte[] compressedData = new byte[ms.Length];
ms.Read(compressedData, , 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[];
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 = ;
while (true)
{
Array.Resize(ref dc_data, totalBytesRead + buffer.Length + );
int bytesRead = zipStream.Read(dc_data,totalBytesRead, buffer.Length);
if (bytesRead == )
{
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结果大小改进

    http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html Web Service 中返回DataSet结果方法: 1)直接返回Da ...

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

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

  3. 《Web开发中让盒子居中的几种方法》

    一.记录下几种盒子居中的方法: 1.0.margin固定宽高居中: 2.0.负margin居中: 3.0.绝对定位居中: 4.0.table-cell居中: 5.0.flex居中: 6.0.trans ...

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

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

  5. 在Web Service中傳送Dictionary

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

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

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

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

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

  8. C语言中返回字符串函数的四种实现方法 2015-05-17 15:00 23人阅读 评论(0) 收藏

    C语言中返回字符串函数的四种实现方法 分类: UNIX/LINUX C/C++ 2010-12-29 02:54 11954人阅读 评论(1) 收藏 举报 语言func存储 有四种方式: 1.使用堆空 ...

  9. C语言中返回字符串函数的四种实现方法

    转自C语言中返回字符串函数的四种实现方法 其实就是要返回一个有效的指针,尾部变量退出后就无效了. 有四种方式: 1.使用堆空间,返回申请的堆地址,注意释放 2.函数参数传递指针,返回该指针 3.返回函 ...

随机推荐

  1. JAVA中继承时方法的重载(overload)与重写/覆写(override)

    JAVA继承时方法的重载(overload)与重写/覆写(override) 重载-Override 函数的方法参数个数或类型不一致,称为方法的重载. 从含义上说,只要求参数的个数或参数的类型不一致就 ...

  2. Dubbo Multicast 注册中心即相关代码实现

    Dubbo 的 Multicast注册中心有下面特点: 不需要启动任何中心节点,只要广播地址一样,就可以互相发现 组播受网络结构限制,只适合小规模应用或开发阶段使用. 组播地址段: 224.0.0.0 ...

  3. android studio 更新 Gradle错误解决方法

    Android Studio每次更新版本都会更新Gradle这个插件,但由于长城的问题每次更新都是失败,又是停止在Refreshing Gradle Project ,有时新建项目的时候报 Gradl ...

  4. 蓝桥杯---地宫取宝(记忆搜索=搜索+dp)

    题目网址:http://lx.lanqiao.org/problem.page?gpid=T120 问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值 ...

  5. SQLSERVER的兼容级别

    今天采用SQL Mannager 2008连接远程的sqlserver数据库,之后弹出一个对话框,修改SQL兼容级别,当时每太注意,一下点击了确定按钮,结果导致两个系统SQL只想全部出错,幸亏发现的早 ...

  6. Mysql中的count()与sum()区别

    首先创建个表说明问题 CREATE TABLE `result` ( `name` varchar(20) default NULL, `subject` varchar(20) default NU ...

  7. Kindle3与亚马逊

    喜欢上亚马逊,偶尔会买些免费或极低价格的书,但始终无法把这些书传到“我的”kindle3上,原因是kindle3无法在中国注册,又绕不开DRM,同时经历了换屏.换主板,早已不是原来的kindle了.今 ...

  8. 搭建Artifactory集群

    搭建Artifactory集群 制品仓库系统有很多,例如Artifactory.Archiva.Sonatype Nexus.Eclipse Package Drone,其中Artifactory拥有 ...

  9. 慕课网-安卓工程师初养成-1-1 Java简介

    来源 http://www.imooc.com/video/1430 主要内容 Java平台应用 核心概念:JVM,JDK,JRE 搭建Java开发环境 使用工具开发安卓程序 经验技巧分享 Java历 ...

  10. linux内核学习(一步一步走)——内核概述

    一.用户空间与内核空间: 用户通过用户空间与操作系统打交道,程序员开发或使用的应用程序位于用户空间.用户空间不能直接访问内核,从而不能访问硬件资源,但是可以通过内核定义的最外层例程——系统调用来访问. ...