1、创建项目,其项目结构如下:
项目结构说明:
DataSetWebService 为Web Service Sites ,用于提供服务;
Test 为windows form 测试程序, 用于测试效果。
服务提供程序代码
DataSetService.asmx
<%@ WebService Language="C#" CodeBehind="DataSetService.asmx.cs" Class="DataSetWebService.DataSetService" %>
DataSetService.asmx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.IO; using System.Data.SqlClient; using System.IO.Compression; using System.Data; using System.Runtime.Serialization.Formatters.Binary; namespace DataSetWebService { /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class DataSetService : System.Web.Services.WebService { [WebMethod(Description="Directly return the dataset object.")] public DataSet GetDataSet() { string sql = "Select * from ShangJianData"; SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=WL_Data;"); sqlConn.Open(); SqlDataAdapter sda = new SqlDataAdapter(sql, sqlConn); DataSet ds = new DataSet("ShangJianData"); sda.Fill(ds); sqlConn.Close(); return ds; } [WebMethod(Description = "Binary formatter.")] 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 = "Dataset Surrogate formatter.")] public byte[] GetDataSetSurrogateBytes() { DataSet ds = GetDataSet(); DataSetSurrogate dss = new DataSetSurrogate(ds); BinaryFormatter ser = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); ser.Serialize(ms, dss); byte[] bytes = ms.ToArray(); return bytes; } [WebMethod(Description = "Dataset Surrogate formatter and then compress array.")] public byte[] GetDataSetSurrogateZipBytes() { DataSet ds = GetDataSet(); DataSetSurrogate dss = new DataSetSurrogate(ds); BinaryFormatter ser = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); ser.Serialize(ms, dss); byte[] bytes = ms.ToArray(); byte[] Zipbuffer = Compress(bytes); return Zipbuffer; } public byte[] Compress(byte[] data) { MemoryStream ms = new MemoryStream(); Stream zipStream = null; zipStream = new GZipStream(ms, CompressionMode.Compress, true); zipStream.Write(data, 0, data.Length); zipStream.Close(); ms.Position = 0; byte[] compressed_data = new byte[ms.Length]; ms.Read(compressed_data, 0,int.Parse(ms.Length.ToString())); return compressed_data; } } }
客户端测试程序代码如下:
Form.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; using System.IO.Compression; using System.Data; using System.Runtime.Serialization.Formatters.Binary; namespace Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void BindDataSet(DataSet ds) { this.dataGridView1.DataSource = ds.Tables[0]; } private void button1_Click(object sender, EventArgs e) { com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient(); DateTime dtBegin = DateTime.Now; DataSet ds = cdw.GetDataSet(); this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin); BindDataSet(ds); } private void button2_Click(object sender, EventArgs e) { com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient(); DateTime dtBegin = DateTime.Now; byte[] buffer = cdw.GetDataSetBytes(); BinaryFormatter ser = new BinaryFormatter(); DataSet ds = ser.Deserialize(new MemoryStream(buffer)) as DataSet; this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "大小:" + buffer.Length.ToString()); BindDataSet(ds); } private void button3_Click(object sender, EventArgs e) { com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient(); DateTime dtBegin = DateTime.Now; byte[] buffer = cdw.GetDataSetSurrogateBytes(); BinaryFormatter ser = new BinaryFormatter(); DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet ds = dss.ConvertToDataSet(); this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin +"大小:" + buffer.Length.ToString()); BindDataSet(ds); } private void button4_Click(object sender, EventArgs e) { com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient(); DateTime dtBegin = DateTime.Now; byte[] zipbuffer = cdw.GetDataSetSurrogateZipBytes(); byte[] buffer = UnZipClass.Decompress(zipbuffer); BinaryFormatter ser = new BinaryFormatter(); DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet ds = dss.ConvertToDataSet(); this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "大小:" + buffer.Length.ToString()); BindDataSet(ds); } } }
UnZipClass.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.IO.Compression; namespace Test { public static class UnZipClass { public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); Stream zipStream = new GZipStream(ms, CompressionMode.Decompress); byte[] dc_data = EtractBytesFormStream(zipStream, data.Length); return dc_data; } catch (System.Exception ex) { return null; } } public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock) { try { byte[] data = null; int totalBytesRead = 0; while (true) { Array.Resize(ref data, totalBytesRead + dataBlock + 1); int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock); if (bytesRead == 0) { break; } totalBytesRead += bytesRead; } Array.Resize(ref data, totalBytesRead); return data; } catch (System.Exception ex) { return null; } } } }