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. NPOI高效匯出Excel

    using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using NPOI. ...

  2. Run Configuration error:broken configuration due to unavailable

    希望大家一起来,毕竟大家都不会使用这个Androidstudio,一起扩展这方面的知识量 http://forums.opengamma.com/t/intellij-code-compiles-bu ...

  3. JDBC中的批量插入和乱码解决

    字符集-乱码问题 用JDBC访问MySql数据库的时候,如果JDBC使用的字符集和MySql使用的字符集不一致,那么会导致乱码发生.解决办法当时是在使用JDBC的时候指定和数据库一样的字符集.我们可以 ...

  4. 使用Semaphore控制并发访问

    Semaphore,信号量. 用在多线程环境下对共享资源访问的一种协调机制. 当一个线程想要访问共享的资源时,这个线程需要获取Semaphore,如果Semaphore内部计数器的值大于0,Semap ...

  5. php文字水印和php图片水印实现代码

    本文章向码农们介绍php文字水印和php图片水印实现代码,需要的码农可以参考一下. php 文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件. 实现 ...

  6. Windows Service的安装卸载 和 Service控制(转)

    Windows Service的安装卸载 和 Service控制 原文地址:http://www.cnblogs.com/Peter-Zhang/archive/2011/10/15/2212663. ...

  7. 获取Repeater控件中的每一项数据

    var items = rptList.Items;//获取Repeater控件的所有项 foreach (RepeaterItem item in items)//遍历每一项内容 {   var t ...

  8. [转]整理索引碎片,提升SQL Server速度

    数据库表A有十万条记录,查询速度本来还可以,但导入一千条数据后,问题出现了.当选择的数据在原十万条记录之间时,速度还是挺快的:但当选择的数据在这一千条数据之间时,速度变得奇慢. 凭经验,这是索引碎片问 ...

  9. android 拍照,裁切,上传圆形头像, 图片等比缩放

    最近太忙了,没有空更新博客,其它部分以后再更新: 今天给大家分享的是解决解析图片的出现oom的问题,我们可以用BitmapFactory这里的各种Decode方法,如果图片很小的话,不会出现oom,但 ...

  10. 关于 LimitedConcurrencyLevelTaskScheduler 的疑惑

    1. LimitedConcurrencyLevelTaskScheduler 介绍 这个TaskScheduler用过的应该都知道,微软开源的一个任务调度器,它的代码很简单, 也很好懂,但是我没有明 ...