WebServices的代碼如下:
using System; using System.Collections.Generic; using System.Web; using System.Web.Services;
using System.Data; using Microsoft.Practices.EnterpriseLibrary.Common; using Microsoft.Practices.EnterpriseLibrary.Data; using System.IO; using System.IO.Compression; using System.Runtime.Serialization.Formatters.Binary; namespace WebService1 { /// <summary> /// Service1 的摘要說明 /// </summary> [WebService(Namespace = " [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class Service1 : System.Web.Services.WebService { [WebMethod(Description="直接返回DataSet對象")] public DataSet GetDataSet() { string sql = "select * from Customers"; Database db = DatabaseFactory.CreateDatabase(); DataSet ds = db.ExecuteDataSet(CommandType.Text,sql); return ds; }
[WebMethod(Description = "返回DataSet對象用Binary序列化后的字節(jié)數(shù)組")] public byte[] GetBytes() { DataSet ds = GetDataSet(); BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, ds); byte[] buffer = ms.ToArray(); return buffer; }
[WebMethod(Description = "返回DataSetSurrogate對象用Binary序列化后的字節(jié)數(shù)組")] public byte[] GetDataSetSurrogateBytes() { DataSet ds = GetDataSet(); DataSetSurrogate dss = new DataSetSurrogate(ds); BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms,dss); byte[] buffer = ms.ToArray(); return buffer; }
[WebMethod(Description = "返回DataSetSurrogate對象用Binary序列化并ZIP壓縮后的字節(jié)數(shù)組")] public byte[] GetDataSetSurrogateZipBytes() { DataSet DS = GetDataSet(); DataSetSurrogate dss = new DataSetSurrogate(DS); BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, dss); byte[] buffer = ms.ToArray(); byte[] Zipbuffer = Compress(buffer); return Zipbuffer; }
//壓縮壓縮后的字節(jié)數(shù)組 public byte[] Compress(byte[] data) { MemoryStream ms = new MemoryStream(); Stream zipStream = new GZipStream(ms, CompressionMode.Compress, true); zipStream.Write(data, 0, data.Length); zipStream.Close(); ms.Position = 0; byte[] buffer = new byte[ms.Length]; ms.Read(buffer, 0,int.Parse(ms.Length.ToString())); return buffer; } } }
客戶端調(diào)用WebServices的代碼如下:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using WebServicesClient.localhost; using System.Data; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Diagnostics; namespace WebServicesClient { public partial class _Default : System.Web.UI.Page { Service1 s = new Service1(); protected void Page_Load(object sender, EventArgs e) {
}
//直接返回DataSet對象 protected void Button1_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); DataSet ds = s.GetDataSet(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop(); Label1.Text = string.Format("耗時:{0}毫秒", sw.ElapsedMilliseconds.ToString()); }
//得到DataSet對象用Binary序列化后的字節(jié)數(shù)組 protected void Button2_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); byte[] buffer = s.GetBytes(); BinaryFormatter bf = new BinaryFormatter(); DataSet ds = bf.Deserialize(new MemoryStream(buffer)) as DataSet; GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop(); Label2.Text = string.Format("耗時:{1}毫秒;數(shù)據(jù)大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString()); } //得到DataSetSurrogate對象用Binary序列化后的字節(jié)數(shù)組 protected void Button3_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); byte[] buffer = s.GetDataSetSurrogateBytes(); BinaryFormatter bf = new BinaryFormatter(); DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet ds = dss.ConvertToDataSet(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop(); Label3.Text = string.Format("耗時:{1}毫秒;數(shù)據(jù)大。簕0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString()); } //得到DataSetSurrogate對象用Binary序列化并ZIP壓縮后的字節(jié)數(shù)組 protected void Button4_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); byte[] zipBuffer = s.GetDataSetSurrogateZipBytes(); byte[] buffer = UnZip.Decompress(zipBuffer); BinaryFormatter bf = new BinaryFormatter(); DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet ds = dss.ConvertToDataSet(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop();
Label4.Text = string.Format("耗時:{1}毫秒;數(shù)據(jù)大。簕0}",zipBuffer.Length.ToString(),sw.ElapsedMilliseconds.ToString()); } } }
測試的結果按照先后順序如下圖所示:
關于測試結果的特殊說明,由于測試環(huán)境是在本地,數(shù)據(jù)量也不是很大,測試的結果離實際情況還不是很接近,如果大家有條件的話,可以測試一下,同時希望把測試的結果提供給大家參考。
最后,為了方便大家,這里還提供了源碼下載,
下載地址如下:WebServiceSummary.rar (點擊下載)
關于源代碼的特殊說明:筆者這里的開發(fā)環(huán)境為VS2008中文版sp1+SQLServer2008sp1。數(shù)據(jù)庫為Northwind數(shù)據(jù)庫。
本文鏈接:http://www.95time.cn/tech/program/2009/6609.asp
出處:深山老林
責任編輯:bluehearts
上一頁 WebServices返回數(shù)據(jù)的4種方法比較 [1] 下一頁
◎進入論壇網(wǎng)絡編程版塊參加討論
|