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;
- }
2.返回DataSet对象用Binary序列化后的字节数组
- [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序列化后的字节数组
- [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压缩后的字节数组
- [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());
- }
-
-
-
-
-
- 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.结果图