这个是个不错的文章,引用一下

Web服务端

 

 

常用WebServices返回数据的4种方法比较(引用)_xml常用WebServices返回数据的4种方法比较(引用)_xml_02Code
using System;
using System.Collections.Generic;
using System.Linq;
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>
    /// 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 Service1 : System.Web.Services.WebService
    {
        [WebMethod(Description = "直接返回DataSet对象")]
        public DataSet GetDataSet()
        {
            string sql = "SELECT TOP 50000 * FROM  dbo.ENTRY_LIST";
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = db.ExecuteDataSet(CommandType.Text, sql);            
            return ds;
        }

        [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
        public byte[] GetBytes()
        {
            //应用场景:1.内网。2.外网且数据量在kb级别时
            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;
        }



        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

 

注意:DataSetSurrogate.dll需要引用

数据库连接配置

 

 

常用WebServices返回数据的4种方法比较(引用)_xml常用WebServices返回数据的4种方法比较(引用)_xml_02Code
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <dataConfiguration defaultDatabase="NorthWind" />
  <connectionStrings>
    <add name="NorthWind" connectionString="Server=.;Database=DB;User Id=sa;Password=sa"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <!-- 
            设置 compilation debug="true" 可将调试符号插入
            已编译的页面中。但由于这会 
            影响性能,因此只在开发过程中将此值 
            设置为 true。
        -->
    <compilation debug="true">
    </compilation>
    <!--
            通过 <authentication> 节可以配置 ASP.NET 用来 
            识别进入用户的
            安全身份验证模式。 
       -->
    <authentication mode="Windows"/>
    <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
  </system.web>
</configuration>

 

客户端调用:

建立解压缩类

 

常用WebServices返回数据的4种方法比较(引用)_xml常用WebServices返回数据的4种方法比较(引用)_xml_02Code
public class UnZip
{
    public static byte[] Decompress(byte[] data)
    {
        try
        {
            MemoryStream ms = new MemoryStream(data);
            Stream zipStream = null;
            zipStream = new GZipStream(ms, CompressionMode.Decompress);
            byte[] dc_data = null;
            dc_data = EtractBytesFormStream(zipStream, data.Length);
            return dc_data;
        }
        catch
        {
            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
        {
            return null;
        }
    }
}

 

界面测试操作:

 

常用WebServices返回数据的4种方法比较(引用)_xml常用WebServices返回数据的4种方法比较(引用)_xml_02Code
public partial class _Default : System.Web.UI.Page 
{
    Service1 s = new Service1();
    protected void Page_Load(object sender, EventArgs e)
    {
       

    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        DataSet ds = s.GetDataSet();
        //GridView1.DataSource = ds.Tables[0].DefaultView;
        //GridView1.DataBind();
        ds.WriteXml("c:\\1234.xml");
        sw.Stop();
        //耗时:23953毫秒
        Label1.Text = string.Format("耗时:{0}毫秒", sw.ElapsedMilliseconds.ToString());
    }
    protected void btn2_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 btn3_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 btn4_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();
        //把结果写到xml文件里面
        ds.WriteXml("c:\\123.xml");
        sw.Stop();
        //耗时:51714毫秒;数据大小:5953476 
        Label4.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", zipBuffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());

    }
}

 

同样需要引用 DataSetSurrogate.dll

学习完成