Dataset与二进制流的转换
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace BaseClientClass { public class XMLClass { /**/ /// <summary> /// 此方法实现对Dataset的序列化生成二进制流 /// </summary> /// <param name="err"></param> /// <returns>dsResult</returns> public byte[] DatasetToBinary( System.Data.DataSet dsResult,ref string err) { //ClearCommand(); //DataSet dsResult = new DataSet(); byte[] bArrayResult = null; try { //dsResult = SqlHelper.ExecuteDataset(m_currentConnectionString, CommandType.Text, m_commandStringBuilder.ToString()); // 上面都是取数据的,无需关心.二进制压缩数据集是下面一小段 dsResult.RemotingFormat = SerializationFormat.Binary; MemoryStream ms = new MemoryStream(); IFormatter bf = new BinaryFormatter(); bf.Serialize(ms, dsResult); bArrayResult = ms.ToArray(); ms.Close(); } catch (Exception ee) { err = ee.ToString(); } return bArrayResult; } /**/ /// <summary> /// 此方法实现从二进制流还原Dataset数据 /// </summary> /// <returns></returns> public DataSet BinaryToDataset(byte[] bUserData) { if (bUserData == null) { //MessageBox.Show("二进制数据流为空"); //err = ""; return null; } // 反序列化的过程 MemoryStream ms = new MemoryStream(bUserData); IFormatter bf = new BinaryFormatter(); object obj = bf.Deserialize(ms); DataSet dsResult = (DataSet)obj; ms.Close(); return dsResult; } } }