准备写个WinCE平台与数据库服务器数据通讯交互方面的专题文章,今天先整理个Web Service通讯方式。
公司目前的硬件产品平台是WinCE5.0,数据通讯是连接服务器与终端的桥梁,关系着终端的数据能否准确及时高效抵达服务器,是整个项目成败的关键。原先公司有同事用VC写过一个程序用Socket进行数据通讯,但一直问题不断。年前我开始探索用SqlCE与SqlServer数据同步方式进行数据上传与下载,通讯已经正常稳定。这方面的文章后续再整理。
Web Service用于PC间通讯的文章网上有很多,但用于WinCE平台调用的经验总结并不多见。Web Service的程序编写与配置调用相对来讲比较简单,Visual Studio里直接新建一个“Asp.net web 服务应用程序”就可以创建一个web Service项目了。其中的代码根据实际需求编写就行,这方面就不详述了。
终端设备是通过GPRS来进行数据传输的,因此,数据流量是非常重要的问题,应当尽可能少的减少数据传输,流量可是Money
,压缩技术是关键。Google**,找到了一款物美价廉的东东-Ihttp://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 所谓物美是这款代码支持Dot net CF平台,所谓价廉是这款代码完全开源免费。
操刀开工。。。先建一个直接返回DataSet集的Web Service服务
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
private SqlConnection Conn;
private string ConnString = "Data Source=(local);Initial Catalog=Northwind;uid=sa;pwd=sa;";
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
dataConnection#region dataConnection
private DataSet GetNorthwindDataSet()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
return ExecuteSql("select * from Employees");
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
private DataSet ExecuteSql(string mysql)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
DataSet dataSet = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(mysql, this.Conn);
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if (this.Conn.State == ConnectionState.Closed)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.Conn.Open();
}
adapter.Fill(dataSet, "table");
}
catch (Exception exception)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
HttpContext.Current.Response.Write(exception.Message);
HttpContext.Current.Response.End();
}
finally
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if ((this.Conn != null) && (this.Conn.State == ConnectionState.Open))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.Conn.Close();
}
adapter.Dispose();
}
return dataSet;
}
#endregion
//方法一:直接返回 DataSet 对象
[WebMethod(Description = "直接返回 DataSet 对象。")]
public DataSet GetDataSet()
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
DataSet dataSet = GetNorthwindDataSet();
return dataSet;
}
建立一个智能设备应用程序,添加Web引用,我这里用的是静态引用,没有用动态引用的原因是,试过网上的动态生成WebService引用的代码,效率远比静态引用要低很多,考虑终端设备资源的有限性,还是用的静态引用。建立好项目后在界面上添加一个button和datagrid控件,添加代码:
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
private webSer.Service1 ws;
private void FrmMain_Load(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
ws = new DeviceApplication1.webSer.Service1();
}
//webmethod直接返回dataset
private void btnDataSet_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.dtGrid.DataSource = null;
DateTime dtBegin = DateTime.Now;
DataSet ds = ws.GetDataSet();
DateTime dtDown = DateTime.Now;
this.dtGrid.DataSource = ds.Tables[0];
MessageBox.Show(string.Format("下载耗时:{0},绑定数据耗时:{1},数据量:{2}",
dtDown - dtBegin, DateTime.Now - dtDown, ds.GetXml().Length));
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
MessageBox.Show(ex.ToString());
}
}
连接好终端设备,测试点击按钮,几秒后DataGrid表格正确显示数据,OK,说明WinCE已经能够正确调用Web Service了。如果不能正确调用,检察WebService发布与Web引用是否正确,数据库配置是否正确。
接下来就是要把数据进行压缩了,压缩前要进行数据的序列化,序列化代码如下:
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// 序列化数据成byte[]
///
/// 未序列化的数据
/// 返回序列化后的byte[]数组
public static byte[] byteXmlSerializer(object o)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (o == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(o.GetType());
System.IO.MemoryStream mem = new MemoryStream();
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(mem, Encoding.Default);
ser.Serialize(writer, o);
writer.Close();
return mem.ToArray();
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw ex;
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// 反序列化数据
///
/// 序列化后的数据
/// 被序列化的数据类型
/// 返回反序列化后的object数据
public static object objXmlDeserialize(byte[] input, Type type)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
XmlSerializer mySerializer = new XmlSerializer(type);
StreamReader stmRead = new StreamReader(new MemoryStream(input), System.Text.Encoding.Default);
return mySerializer.Deserialize(stmRead);
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw ex;
}
}
ICSharpCode提供了多种数据压缩的方式,我这里测试了四种方式:BZip,Deflate,GZip,Zip
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
private static int buffSize = 2048;//指定压缩块缓存的大小,一般为2048的倍数
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// BZIP2压缩数据
///
/// 原始未压缩数据
/// 压缩后的byte[]数据
public static byte[] BZipCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
//int buffSize = 2048;//指定压缩块的大小,一般为2048的倍数
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (MemoryStream inmsStrm = new MemoryStream(input))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
BZip2.Compress(inmsStrm, outmsStrm, buffSize);
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw ex;
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// 解压缩BZIP2数据
///
/// 被BZIP2压缩过的byte[]数据
/// 解压后的byte[]数据
public static byte[] BZipDeCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (MemoryStream inmsStrm = new MemoryStream(input))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
BZip2.Decompress(inmsStrm, outmsStrm);
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw (ex);
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// 压缩Deflater数据
///
/// 待压缩byte[]数据
/// 返回压缩后的byte[]
public static byte[] DeflaterCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Deflater mDeflater = new Deflater(Deflater.BEST_COMPRESSION);
//int buffSize = 2048;//131072 buff size
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (DeflaterOutputStream mStream = new DeflaterOutputStream(outmsStrm, mDeflater, buffSize))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
mStream.Write(input, 0, input.Length);
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw (ex);
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// 解压缩Deflater数据
///
/// 压缩过的byte[]数据
/// 解压后的byte[]数据
public static byte[] DeflaterDeCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Int32 mSize;
//int buffSize = 2048;
byte[] buff = new byte[buffSize];
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (InflaterInputStream mStream = new InflaterInputStream(new MemoryStream(input)))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
while (true)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
mSize = mStream.Read(buff, 0, buff.Length);
if (mSize > 0)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
outmsStrm.Write(buff, 0, mSize);
}
else
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
break;
}
}
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw (ex);
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// GZIP压缩
///
/// 未压缩的数据
/// GZIP压缩后的数据
public static byte[] GZipCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (GZipOutputStream gzip = new GZipOutputStream(outmsStrm))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
gzip.Write(input, 0, input.Length);
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw (ex);
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// GZIP解压缩
///
/// 压缩过的数据
/// 解压后的数据
public static byte[] GZipDeCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (GZipInputStream gzip = new GZipInputStream(new MemoryStream(input)))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Int32 mSize;
//int buffSize = 2048;
byte[] buff = new byte[buffSize];
while (true)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
mSize = gzip.Read(buff, 0, buffSize);
if (mSize > 0)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
outmsStrm.Write(buff, 0, mSize);
}
else
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
break;
}
}
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw (ex);
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// ZIP压缩数据
///
/// 待压缩的数据
/// ZIP压缩后的数据
public static byte[] ZipCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (ZipOutputStream zipStrm = new ZipOutputStream(outmsStrm))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
ZipEntry zn = new ZipEntry("znName");
zipStrm.PutNextEntry(zn);
zipStrm.Write(input, 0, input.Length);
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw (ex);
}
}
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**////
/// ZIP解压缩数据
///
/// 压缩过的数据
/// 解压后的数据
public static byte[] ZipDeCompress(byte[] input)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (input == null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw new ArgumentNullException("null input");
}
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (MemoryStream outmsStrm = new MemoryStream())
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
using (ZipInputStream zipStrm = new ZipInputStream(new MemoryStream(input)))
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
Int32 mSize;
//int buffSize = 2048;
byte[] buff = new byte[buffSize];
ZipEntry zn = new ZipEntry("znName");
while ((zn = zipStrm.GetNextEntry()) != null)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
while (true)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
mSize = zipStrm.Read(buff, 0, buffSize);
if (mSize > 0)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
outmsStrm.Write(buff, 0, mSize);
}
else
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
break;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
}
}
return outmsStrm.ToArray();
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
throw (ex);
}
}
添加WebService服务
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> //方法二:返回 DataSet 对象用序列化后的byte[]字节数组
[WebMethod(Description = "返回 DataSet 对象用序列化后的byte[]字节数组。")]
public byte[] GetDataSetBytes()
{
DataSet dataSet = GetNorthwindDataSet();
return ComZipClass.zip.byteXmlSerializer(dataSet);
}
//方法三返回 DataSet对象用序列化并Bzip压缩后的byte[]字节数组
[WebMethod(Description = "返回 DataSet对象用序列化并Bzip压缩后的byte[]字节数组。")]
public byte[] GetBZiipCompress()
{
DataSet dataSet = GetNorthwindDataSet();
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
byte[] zip = ComZipClass.zip.BZipCompress(ser);
return zip;
}
[WebMethod(Description = "返回Deflater压缩")]
public byte[] GetDeflaterCompress()
{
DataSet dataSet = GetNorthwindDataSet();
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
//byte[] ser = ZipClass.zip.byteXmlSerializer(dataSet);
byte[] zip = ComZipClass.zip.DeflaterCompress(ser);
return zip;
}
[WebMethod(Description = "返回Gzip压缩")]
public byte[] GetGZipCompress()
{
DataSet dataSet = GetNorthwindDataSet();
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
byte[] zip = ComZipClass.zip.GZipCompress(ser);
return zip;
}
[WebMethod(Description = "返回Zip压缩")]
public byte[] GetZipCompress()
{
DataSet dataSet = GetNorthwindDataSet();
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
byte[] zip = ComZipClass.zip.ZipCompress(ser);
return zip;
}
添加终端设备调用
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
//webmethod序列化数据
private void btnSerial_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.dtGrid.DataSource = null;
DataSet ds = new DataSet();
DateTime dtBegin = DateTime.Now;
byte[] datas = ws.GetDataSetBytes();
DateTime dtDown = DateTime.Now;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ds = (DataSet)ComZipClass.zip.objXmlDeserialize(datas, typeof(DataSet));
DateTime dtSerial = DateTime.Now;
this.dtGrid.DataSource = ds.Tables[0];
MessageBox.Show(string.Format("下载耗时:{0},解压序列化数据耗时:{1},绑定数据耗时:{2},数据量:{3}",
dtDown - dtBegin,dtSerial-dtDown, DateTime.Now - dtDown, datas.Length));
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
MessageBox.Show(ex.ToString());
}
}
//序列化并压缩BZIP后数据
private void btnBZip_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.dtGrid.DataSource = null;
DataSet ds = new DataSet();
DateTime dtBegin = DateTime.Now;
byte[] datas = ws.GetBZiipCompress();
DateTime dtDown = DateTime.Now;
byte[] uzip = ComZipClass.zip.BZipDeCompress(datas);
DateTime dtUnzip = DateTime.Now;
ds = (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
DateTime dtSerial = DateTime.Now;
this.dtGrid.DataSource = ds.Tables[0];
MessageBox.Show(string.Format("下载耗时:{0},解压BZIP耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
dtDown - dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
MessageBox.Show(ex.ToString());
}
}
//序列化并压缩Deflate后数据
private void btnDeflater_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.dtGrid.DataSource = null;
DataSet ds = new DataSet();
DateTime dtBegin = DateTime.Now;
byte[] datas = ws.GetDeflaterCompress();
DateTime dtDown = DateTime.Now;
byte[] uzip = ComZipClass.zip.DeflaterDeCompress(datas);
DateTime dtUnzip = DateTime.Now;
ds = (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
DateTime dtSerial = DateTime.Now;
this.dtGrid.DataSource = ds.Tables[0];
MessageBox.Show(string.Format("下载耗时:{0},解压Deflater耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
dtDown - dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
MessageBox.Show(ex.ToString());
}
}
//序列化并压缩GZIP后数据
private void btnGZip_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.dtGrid.DataSource = null;
DataSet ds = new DataSet();
DateTime dtBegin = DateTime.Now;
byte[] datas = ws.GetGZipCompress();
DateTime dtDown = DateTime.Now;
byte[] uzip = ComZipClass.zip.GZipDeCompress(datas);
DateTime dtUnzip = DateTime.Now;
ds = (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
DateTime dtSerial = DateTime.Now;
this.dtGrid.DataSource = ds.Tables[0];
MessageBox.Show(string.Format("下载耗时:{0},解压GZIP耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
dtDown - dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
MessageBox.Show(ex.ToString());
}
}
//序列化并压缩ZIP后数据
private void btnZip_Click(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
try
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this.dtGrid.DataSource = null;
DataSet ds = new DataSet();
DateTime dtBegin = DateTime.Now;
byte[] datas = ws.GetZipCompress();
DateTime dtDown = DateTime.Now;
byte[] uzip = ComZipClass.zip.ZipDeCompress(datas);
DateTime dtUnzip = DateTime.Now;
ds = (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
DateTime dtSerial = DateTime.Now;
this.dtGrid.DataSource = ds.Tables[0];
MessageBox.Show(string.Format("下载耗时:{0},解压ZIP耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
dtDown - dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
MessageBox.Show(ex.ToString());
}
}
几个测试压缩数据dataset :268128;serial:269199;bzip:94561;Deflater:107049;gzip:108276;zip:108408;
附上源码,解压密码暂不公开,有需要的留下Email。源码一 源码二
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21255398/viewspace-604875/,如需转载,请注明出处,否则将追究法律责任。