首页 > 代码库 > .net WebService 大数据量时性能的提高
.net WebService 大数据量时性能的提高
1.直接返回DataSet对象
C#代码
- [WebMethod(Description = "直接返回DataSet对象")]
- public DataSet GetUserListDateSet()
- {
- SqlConnection sqlCon = new SqlConnection("Data Source=.,1444;Initial Catalog=StudyNet;Persist Security Info=True;User ID=**;Password=******");
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = sqlCon;
- cmd.CommandText = "select * from T_User";
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- return ds;
- }
2.返回DataSet对象用Binary序列化后的字节数组
C#代码
- [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
- public byte[] GetUserListDateSetBytes()
- {
- DataSet ds = GetUserListDateSet();
- //序列化
- BinaryFormatter ser = new BinaryFormatter();
- MemoryStream stream = new MemoryStream();
- ser.Serialize(stream, ds);
- return stream.GetBuffer();
- }
3.返回DataSetSurrogate对象用Binary序列化后的字节数组
C#代码
- [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
- public byte[] GetUserListDateSetSurrogateBytes()
- {
- DataSet ds = GetUserListDateSet();
- DataSetSurrogate dss = new DataSetSurrogate(ds);
- //序列化
- BinaryFormatter ser = new BinaryFormatter();
- MemoryStream stream = new MemoryStream();
- ser.Serialize(stream, dss);
- return stream.GetBuffer();
- }
4.返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组
C#代码
- [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组")]
- public byte[] GetUserListDateSetSurrogateZipBytes()
- {
- DataSet ds = GetUserListDateSet();
- DataSetSurrogate dss = new DataSetSurrogate(ds);
- //序列化
- BinaryFormatter ser = new BinaryFormatter();
- MemoryStream stream = new MemoryStream();
- ser.Serialize(stream, dss);
- //压缩
- return Compress(stream.ToArray());
- }
- /// <summary>
- /// 压缩数据
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public byte[] Compress(byte[] data)
- {
- MemoryStream ms = new MemoryStream();
- GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress);
- zipStream.Write(data, 0, data.Length);//将数据压缩并写到基础流中
- zipStream.Close();
- return ms.ToArray();
- }
5.结果图
- .net_webservice大数量时性能的提供.rar (119.2 KB)
- 下载次数: 26
- DataSetSurrogate.rar (8.7 KB)
.net WebService 大数据量时性能的提高
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。