博客园  :: 首页  :: 新随笔  :: 联系 :: 管理
webservice服务端方法
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.IO.Compression;
  10. using System.Data.SqlClient;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. namespace Test
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         private void BindDataSet(DataSet DS)
  21.         {
  22.             this.dataGridView1.DataSource = DS.Tables[0];
  23.         }
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
  27.             DateTime dtBegin = DateTime.Now;
  28.             DataSet DS = ds.GetDataSet();
  29.             this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
  30.             BindDataSet(DS);
  31.         }
  32.         private void button2_Click(object sender, EventArgs e)
  33.         {
  34.             com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
  35.             DateTime dtBegin = DateTime.Now;
  36.             byte[] buffer = ds.GetDataSetBytes();
  37.             DataSet DS = ds.GetDataSet();
  38.             BinaryFormatter ser = new BinaryFormatter();
  39.             DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet;
  40.             this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + buffer.Length.ToString());
  41.             BindDataSet(DS);
  42.         }
  43.         private void button3_Click(object sender, EventArgs e)
  44.         {
  45.             com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
  46.             DateTime dtBegin = DateTime.Now;
  47.             byte[] buffer = ds.GetDataSetSurrogateBytes();
  48.             BinaryFormatter ser = new BinaryFormatter();
  49.             DataSet DS = ds.GetDataSet();
  50.             DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
  51.             DataSet dataset = dss.ConvertToDataSet();
  52.             this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + buffer.Length.ToString());
  53.             BindDataSet(DS);
  54.         }
  55.         private void button4_Click(object sender, EventArgs e)
  56.         {
  57.             com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
  58.             DateTime dtBegin = DateTime.Now;
  59.             byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();
  60.             byte[] buffer = UnZipClass.Decompress(zipBuffer);
  61.             BinaryFormatter ser = new BinaryFormatter();
  62.             DataSet DS = ds.GetDataSet();
  63.             DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
  64.             DataSet dataset = dss.ConvertToDataSet();
  65.             this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + zipBuffer.Length.ToString());
  66.             BindDataSet(DS);
  67.         }
  68.     }
  69. }

 

调用端:

  1. using System;
  2. using System.Data;
  3. using System.Web;
  4. using System.Collections;
  5. using System.Web.Services;
  6. using System.Web.Services.Protocols;
  7. using System.ComponentModel;
  8. using System.IO;
  9. using System.IO.Compression;
  10. using System.Data.SqlClient;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. namespace DataSetWebService
  13. {
  14.     /// <summary>
  15.     /// Service1 的摘要说明
  16.     /// </summary>
  17.     [WebService(Namespace = "http://tempuri.org/")]
  18.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  19.     [ToolboxItem(false)]
  20.     public class DataSetService : System.Web.Services.WebService
  21.     {
  22.         [WebMethod(Description="直接返回DataSet对象")]
  23.         public DataSet GetDataSet()
  24.         {
  25.             //http://www.dzbsoft.com  XT_TEXT
  26.             string sql = "select * from XT_TEXT";
  27.             SqlConnection conn = new SqlConnection("Server=60.28.25.58;DataBase=s168593;user id=s168593;password=h0y+FeC*;");
  28.             conn.Open();
  29.             SqlDataAdapter dataAd = new SqlDataAdapter(sql, conn);
  30.             DataSet DS = new DataSet("XT_TEXT");
  31.             dataAd.Fill(DS);
  32.             conn.Close();
  33.             return DS;
  34.         }
  35.         [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
  36.         public byte[] GetDataSetBytes()
  37.         {
  38.             DataSet DS = GetDataSet();
  39.             BinaryFormatter ser = new BinaryFormatter();
  40.             MemoryStream ms = new MemoryStream();
  41.             ser.Serialize(ms, DS);
  42.             byte[] buffer = ms.ToArray();
  43.             return buffer;
  44.         }
  45.         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
  46.         public byte[] GetDataSetSurrogateBytes()
  47.         {
  48.             DataSet DS = GetDataSet();
  49.             DataSetSurrogate dss = new DataSetSurrogate(DS);
  50.             BinaryFormatter ser = new BinaryFormatter();
  51.             MemoryStream ms = new MemoryStream();
  52.             ser.Serialize(ms, dss);
  53.             byte[] buffer = ms.ToArray();
  54.             return buffer;
  55.         }
  56.         [WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组")]
  57.         public byte[] GetDataSetSurrogateZipBytes()
  58.         {
  59.             DataSet DS = GetDataSet();
  60.             DataSetSurrogate dss = new DataSetSurrogate(DS);
  61.             BinaryFormatter ser = new BinaryFormatter();
  62.             MemoryStream ms = new MemoryStream();
  63.             ser.Serialize(ms, dss);
  64.             byte[] buffer = ms.ToArray();
  65.             byte[] Zipbuffer = Compress(buffer);
  66.             return Zipbuffer;
  67.         }
  68.         public byte[] Compress(byte[] data)
  69.         {
  70.             MemoryStream ms = new MemoryStream();
  71.             Stream zipStream = null;
  72.             zipStream = new GZipStream(ms, CompressionMode.Compress, true);
  73.             zipStream.Write(data, 0, data.Length);
  74.             zipStream.Close();
  75.             ms.Position = 0;
  76.             byte[] compressed_data = new byte[ms.Length];
  77.             ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
  78.             return compressed_data;
  79.         }
  80.     }
  81. }

调用的时候的解压缩方法:

 

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.IO.Compression;
  6. namespace Test
  7. {
  8.     public static class UnZipClass
  9.     {
  10.         /// <summary>
  11.         /// Decompresses the specified data.
  12.         /// </summary>
  13.         /// <param name="data">The data.</param>
  14.         /// <returns></returns>
  15.         public static byte[] Decompress(byte[] data)
  16.         {
  17.             try
  18.             {
  19.                 MemoryStream ms = new MemoryStream(data);
  20.                 Stream zipStream = null;
  21.                 zipStream = new GZipStream(ms, CompressionMode.Decompress);
  22.                 byte[] dc_data = null;
  23.                 dc_data = EtractBytesFormStream(zipStream, data.Length);
  24.                 return dc_data;
  25.             }
  26.             catch
  27.             {
  28.                 return null;
  29.             }
  30.         }
  31.         public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
  32.         {
  33.             try
  34.             {
  35.                 byte[] data = null;
  36.                 int totalBytesRead = 0;
  37.                 while (true)
  38.                 {
  39.                     Array.Resize(ref data, totalBytesRead + dataBlock + 1);
  40.                     int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
  41.                     if (bytesRead == 0)
  42.                     {
  43.                         break;
  44.                     }
  45.                     totalBytesRead += bytesRead;
  46.                 }
  47.                 Array.Resize(ref data, totalBytesRead);
  48.                 return data;
  49.             }
  50.             catch
  51.             {
  52.                 return null;
  53.             }
  54.         }
  55.     }
  56. }