.net WebService 大数据量时性能的提高
1.直接返回DataSet对象
- [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;
- }
- [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();
- }
- [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();
- }
- [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();
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。