首页 技术 正文
技术 2022年11月15日
0 收藏 816 点赞 2,896 浏览 7058 个字

http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html

Web Service 中返回DataSet结果方法:

1)直接返回DataSet对象
    特点:通常组件化的处理机制,不加任何修饰及处理;
    优点:代码精减、易于处理,小数据量处理较快;
    缺点:大数据量的传递处理慢,消耗网络资源;
    建议:当应用系统在内网、专网(局域网)的应用时,或外网(广域网)且数据量在KB级时的应用时,采用此种模式。

2)返回DataSet对象用Binary序列化后的字节数组
    特点:字节数组流的处理模式;
    优点:易于处理,可以中文内容起到加密作用;
    缺点:大数据量的传递处理慢,较消耗网络资源;
    建议:当系统需要进行较大数据交换时采用。

3)返回DataSetSurrogate对象用Binary序列化后的字节数组
    特点:微软提供的开源组件;下载地址: http://support.microsoft.com/kb/829740/zh-cn
    优点:易于处理,可以中文内容起到加密作用;
    缺点:大数据量的传递处理慢,较消耗网络资源;
    建议:当系统需要进行较大数据交换时采用。

4)返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
    特点:对字节流数组进行压缩后传递;
    优点:当数据量大时,性能提高效果明显,压缩比例大;
    缺点:相比第三方组件,压缩比例还有待提高;
    建议:当系统需要进行大数据量网络数据传递时,建议采用此种可靠、高效、免费的方法。

WebService代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;namespace DataSetWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class DataSetService : System.Web.Services.WebService
    {
        string connectionString = "Server=.;DataBase=NORTHWIND;user id=sa;password=sa;";        [WebMethod(Description = "直接返回DataSet对象")]
        public DataSet GetDataSet()
        {
            DataSet DS = new DataSet();
            string sql = "SELECT * FROM [Customers]";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlDataAdapter dataAd = new SqlDataAdapter(sql, conn);
               
                dataAd.Fill(DS);
            }
            return DS;
        }        [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
        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 = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
        public byte[] GetDataSetSurrogateBytes()
        {
            DataSet ds = GetDataSet();
            DataSetSurrogate dss = new DataSetSurrogate(ds);
            BinaryFormatter ser = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            ser.Serialize(ms, dss);
            byte[] buffer = ms.ToArray();
            return buffer;
        }        [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
        public byte[] GetDataSetSurrogateZipBytes()
        {
            DataSet DS = GetDataSet();
            DataSetSurrogate dss = new DataSetSurrogate(DS);
            BinaryFormatter ser = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            ser.Serialize(ms, dss);
            byte[] buffer = ms.ToArray();
            byte[] zipBuffer = Compress(buffer);
            return zipBuffer;
        }        /// <summary>
        /// 压缩二进制数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            Stream zipStream = null;
            using (zipStream = new GZipStream(ms, CompressionMode.Compress, true))
            {
                zipStream.Write(data, 0, data.Length);              
                zipStream.Close();
            }
            ms.Position = 0;
            byte[] compressedData = new byte[ms.Length];
            ms.Read(compressedData, 0, int.Parse(ms.Length.ToString()));
            return compressedData;
        }
    }
}

调用代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        }
        WService.DataSetServiceSoapClient _Ds = new WService.DataSetServiceSoapClient();        private void BindDataSet(DataSet DS)
        {
            this.GridView1.DataSource = DS.Tables[0];
            this.GridView1.DataBind();
        }        /// <summary>
        ///调用 直接返回DataSet对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
           DataSet ds= _Ds.GetDataSet();
           BindDataSet(ds);
        }        /// <summary>
        /// 调用  返回DataSet对象用Binary序列化后的字节数组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            byte[] buffer = _Ds.GetDataSetBytes();
            BinaryFormatter bf = new BinaryFormatter();
            DataSet ds = bf.Deserialize(new MemoryStream(buffer)) as DataSet;
            BindDataSet(ds);
            Label2.Text = buffer.Length.ToString();
        }        /// <summary>
        /// 调用  返回DataSetSurrogate对象用Binary序列化后的字节数组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            byte[] buffer = _Ds.GetDataSetSurrogateBytes();
            BinaryFormatter bf = new BinaryFormatter();
            DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
            DataSet ds = dss.ConvertToDataSet();
            BindDataSet(ds);
            Label3.Text = buffer.Length.ToString();
        }        /// <summary>
        /// 调用  返回DataSetSurrogate对象用Binary序列化后的字节数组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button4_Click(object sender, EventArgs e)
        {
            byte[] buffer = _Ds.GetDataSetSurrogateZipBytes();
            MemoryStream ms = new MemoryStream(buffer);
            Stream zipStream = new GZipStream(ms, CompressionMode.Decompress);
            byte[] dc_data = null;
            int totalBytesRead = 0;
            while (true)
            {
                Array.Resize(ref dc_data, totalBytesRead + buffer.Length + 1);
                int bytesRead = zipStream.Read(dc_data,totalBytesRead, buffer.Length);
                if (bytesRead == 0)
                {
                    break;
                }
                totalBytesRead += bytesRead;
            }
            BinaryFormatter bf = new BinaryFormatter();
            DataSetSurrogate dss = bf.Deserialize(new MemoryStream(dc_data)) as DataSetSurrogate;
            DataSet ds = dss.ConvertToDataSet();
            BindDataSet(ds);
            Label4.Text = buffer.Length.ToString();
        }
    }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905