WebService使用DataSet -- 用BinaryFormatter 序列化减少流的大小

WebService中的DataSet序列化使用

 

 

服务端代码:

============================================================

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters.Binary;
using CompressDataSet;
using System.IO;

namespace WebService1
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod(Description = "获取DataSet,并进行操作")]
        public bool BackUpUserTable(byte[] ds)
        {
            //获取用户传来的DataSet序列化 并反序列化
            MemoryStream ms = new MemoryStream(ds);
            BinaryFormatter bf = new BinaryFormatter();
            Object o = bf.Deserialize(ms);
            DataSetSurrogate dss = (DataSetSurrogate)o;
            DataSet dataSet = dss.ConvertToDataSet();
            //自定义操作内容
            //    ***
            return false;
        }
    }
}

 

 

 

 

 

客户端代码:

================================================================

 

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 CompressDataSet;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 更新服务端数据库方法
        /// </summary>
        /// <param name="userName"></param>
        public void UpdateInfo()
        {
            //封装DataSet
            DataSet ds = new DataSet();
            DataTable table = new DataTable();
            table.Columns.Add("a");
            table.Columns.Add("b");
            table.Columns.Add("c");
            for (int i = 0; i < 10; i++)
            {
                DataRow row = table.NewRow();
                row["a"] = i.ToString() + "a";
                row["b"] = i.ToString() + "b";
                row["c"] = i.ToString() + "c";
            }
            //把DataSet序列化
            DataSetSurrogate dss = new DataSetSurrogate(ds);
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, dss);
            //把流装到数组里
            byte[] io = ms.ToArray();
            //调用WebService
            ServiceReferenceUserService.Service1SoapClient userService = new WindowsClient.ServiceReferenceUserService.Service1SoapClient();

            //操作
            if (userService.BackUpUserTable(io))
            {
                MessageBox.Show("成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}

 

需要引用:

DataSetSurrogate.dll 下载地址:http://download.csdn.net/source/2294498

 

 

转载结论:

===========================================================

确实用了BinaryFormatter 这种方式实现了提高效率.
再者,用微软提供的DataSetSurrogate 类可以此基础上进一步压缩数据大小,DataSetSurrogate 在.net 2.0里自带。这是比较结果.

 SoapFormatterBinaryFormatter
Dataset 序列化後Bytes數1,953,0781,448,399
DataSetSurrogate 序列化後Bytes數2,371,942575,684

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值