Web Service 中返回DataSet结果的几种方法
2024-09-24 10:25:58
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();
}
}
}
最新文章
- PADS Layout 颜色设置
- Android ant自动打包 crunch 报错
- redhad借用CentOs yum 安装
- 为什么接口要规定成员变量必须是public static final的呢?(转)
- (转)传统MySQL+ Memcached架构遇到的问题
- [swustoj 191] 迷宫逃离
- [LeetCode228]Summary Ranges
- Java开发笔记(二十二)神奇的冒号
- Log4j日志框架学习零到壹(一)
- 使用python脚本批量删除阿里云oss中的mp4文件
- C/S与B/S的区别
- Eclipse调试DEBUG时快速查看某个变量的值的快捷键、快速跳转到某行的快捷键
- web框架-Struts开始
- 移动app传统测试流程优化
- 分页功能实现之通过ajax实现表单内容刷新
- NSTimer、performSelector 函数没有被调用的原因
- JAVA 设计模式之原型模式
- CTF西湖论剑
- windows远程连接Ubuntu12.04 (Ubuntu14.0连接方式不一样)
- golang构造函数
热门文章
- Intellij IDEA 使用Spring-boot-devTools无效解决办法
- javascript最新深度克隆对象方法
- shell test -n -z
- oninput 属性
- spring mvc中的json整合
- HDU 5877 [dfs序][线段树][序]
- (转)C#执行exe程序
- MyEclipse 6.5 破解文件代码
- Hibernate三种状态的区分,以及save,update,saveOrUpdate,merge等的使用 引自http://www.blogjava.net/TiGERTiAN/archive/2008/10/25/236519.html
- jdk集合常用方法分析之HashSet和TreeSet