首页 技术 正文
技术 2022年11月12日
0 收藏 898 点赞 3,850 浏览 8386 个字

直接返回DataSet对象
返回DataSet对象用Binary序列化后的字节数组
返回DataSetSurrogate对象用Binary序列化后的字节数组
返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
案例

直接返回DataSet对象


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

返回DataSet对象用Binary序列化后的字节数组


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

返回DataSetSurrogate对象用Binary 序列化后的字节数组


特点:微软提供的开源组件;

下载地址:

http://support.microsoft.com/kb/829740/zh-cn

优点:易于处理,可以中文内容起到加密作用;
缺点:大数据量的传递处理慢,较消耗网络资源;
建议:当系统需要进行较大数据交换时采用。

返回DataSetSurrogate对象用Binary 序列化并Zip压缩后的字节数组


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

隐藏行号 复制代码 ? Web.config

  1.   <appSettings>
  2.     <add key="ConnectionStringAccounts" value="server=.;database=MyWebServices;uid=sa;pwd=sa123"/>
  3.   </appSettings>

<!–
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: “Courier New”,”Consolas”,”Fixedsys”,courier,monospace,serif}
.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
.number_show{ padding-left:52px !important; list-style:decimal outside !important}
.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
.number_hide{ padding-left:0px !important; list-style-type:none !important}
.number_hide li{ list-style-type:none !important; border-left:0px}
ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
ol.mainarea li pre.alt{ background-color:#f7f7ff !important}
–>

添加Web引用。

WebService – 怎样提高WebService性能 大数据量网络传输处理

案例:

WebService 代码

 using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary; //二进制序列化和反序列化 namespace DBZWebService
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class SoftService : System.Web.Services.WebService
{ [WebMethod(Description="直接返回DataSet")]
public DataSet GetDataSet()
{
string siteName = ConfigurationManager.AppSettings["ConnectionStringAccounts"];
string sql = "select * from XT_TEXT_LX";
SqlConnection conn = new SqlConnection(siteName);
conn.Open();
SqlDataAdapter adpter = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet("XT_TEXT_LX");
adpter.Fill(ds);
conn.Close();
return ds;
} [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
public byte[] GetDataSetBinary()
{
DataSet ds = new 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 = new DataSet();
ds = GetDataSet(); //使用DataSetSurrogate
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序列化并Zip压缩后的字节数组")]
public byte[] GetDataSetSurrogateZipBytes()
{
DataSet ds = new 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 = Comperss(buffer);
return Zipbuffer;
} public byte[] Comperss(byte[] data)
{
MemoryStream ms = new MemoryStream();
Stream zipStream = null;
zipStream = new GZipStream(ms, CompressionMode.Compress, true);
zipStream.Write(data, , data.Length);
zipStream.Close();
ms.Position = ;
byte[] comperssed_data = new byte[ms.Length];
ms.Read(comperssed_data, , int.Parse(ms.Length.ToString()));
return comperssed_data; }
}
}

SoftService.asmx

WebService – 怎样提高WebService性能 大数据量网络传输处理

WebService – 怎样提高WebService性能 大数据量网络传输处理

cs代码

 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 System.Web.Services;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary; //二进制序列化和反序列化 namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void BindDataSet(DataSet ds)
{
this.dataGridView1.DataSource = ds.Tables[];
} /// <summary>
/// 直接返回DataSet对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
SoftService.SoftService dss = new SoftService.SoftService(); DateTime dtBegin = DateTime.Now;
DataSet ds = dss.GetDataSet();
this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
BindDataSet(ds);
} /// <summary>
/// 返回DataSet对象用Binary序列化后的字节数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
SoftService.SoftService dss = new SoftService.SoftService();
DateTime dtBegin = DateTime.Now;
byte[] buffer = dss.GetDataSetBinary();
DataSet ds = dss.GetDataSet();
BinaryFormatter ser = new BinaryFormatter(); //反序列化为DataSet
DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet; this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
BindDataSet(ds);
} /// <summary>
/// 返回DataSetSurrogate对象用Binary序列化后的字节数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
SoftService.SoftService ss = new SoftService.SoftService();
DateTime dtBegin = DateTime.Now;
byte[] buffer = ss.GetDataSetSurrogateBytes();
BinaryFormatter ser = new BinaryFormatter(); //使用Micorsoft组件DataSetSurrogate反序列化
DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
//使用ConvertToDataSet转化为DataSet
DataSet dataset = dss.ConvertToDataSet(); this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
BindDataSet(dataset);
} /// <summary>
/// 返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
SoftService.SoftService ss = new SoftService.SoftService();
DateTime dtBegin = DateTime.Now; byte[] zipBuffer = ss.GetDataSetSurrogateZipBytes();
//使用类DeCompress的解压缩方法
byte[] buffer = DeCompress.Decompress(zipBuffer); BinaryFormatter ser = new BinaryFormatter();
//使用Micorsoft组件DataSetSurrogate反序列化
DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
//使用ConvertToDataSet转化为DataSet
DataSet dataset = dss.ConvertToDataSet(); this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin+" " + zipBuffer.Length.ToString());
BindDataSet(dataset);
}
}
}

Form1.cs

解压类代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression; namespace Test
{
class DeCompress
{
/// <summary>
/// 解压缩
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
Stream zipStram = null;
zipStram = new GZipStream(ms, CompressionMode.Decompress);
byte[] dc_data = null;
dc_data = EtractBytesFormStream(zipStram, data.Length);
return dc_data;
}
catch
{ return null;
} } public static byte[] EtractBytesFormStream(Stream zipStream,int dataBlock)
{ try
{
byte[] data = null;
int totalBytesRead = ;
while (true)
{
Array.Resize(ref data, totalBytesRead + dataBlock + );
int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
if (bytesRead == )
{
break;
}
totalBytesRead += bytesRead;
}
Array.Resize(ref data, totalBytesRead);
return data;
}
catch
{ return null;
}
}
}
}

DeCompress.cs

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290