-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Services;
-
using System.Data;
-
using System.Runtime.Serialization.Formatters.Binary;
-
using System.IO;
-
using System.IO.Compression;
-
using System.Text;
-
using DAL;
-
-
/// <summary>
-
///WebServiceTest 的摘要说明
-
/// </summary>
-
[WebService(Namespace = "http://tempuri.org/")]
-
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
-
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
-
// [System.Web.Script.Services.ScriptService]
-
public class WebServiceTest : System.Web.Services.WebService {
-
-
public WebServiceTest () {
-
-
//如果使用设计的组件,请取消注释以下行
-
//InitializeComponent();
-
}
-
-
[WebMethod]
-
public string HelloWorld() {
-
return "Hello World";
-
}
-
[WebMethod(Description="直接返回DataSet对象")]
-
public DataSet GetDataSet()
-
{
-
StringBuilder builder = new StringBuilder();
-
builder.Append("select * from ServiceInfo");
-
DataSet ds = SQLHelper.Query(builder.ToString());
-
return ds;
-
}
-
[WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
-
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序列化后的字节数组")]
-
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压缩后的字节数组")]
-
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;
-
}
-
//压缩压缩后的字节数组
-
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;
-
}
-
}
-
<%@ Page Language="C#" CodeFile="WebTest.aspx.cs" Inherits="WebTest" %>
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title></title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
-
Text="直接得到DataSet对象" />
-
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
-
Text="得到DataSet对象用Binary序列化后的字节数组" />
-
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
-
style="height: 26px" Text="得到DataSetSurrogate对象用Binary序列化后的字节数组" />
-
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
-
Text="得到DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组" />
-
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:GridView ID="GridView1" runat="server" Width="100%">
-
</asp:GridView>
-
</div>
-
</form>
-
</body>
-
</html>
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Data;
-
using System.Diagnostics;
-
using System.Runtime.Serialization.Formatters.Binary;
-
using WebServicesClient;
-
using System.IO;
-
-
-
public partial class WebTest : System.Web.UI.Page
-
{
-
WebServiceTest s = new WebServiceTest();
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
}
-
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());
-
}
-
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}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
-
}
-
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}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
-
}
-
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}毫秒;数据大小:{0}", zipBuffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
-
}
-
}