扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
/**//// <summary> /// Method for users data query with binaryFormatter /// </summary> /// <param name="err"></param> /// <returns></returns> public byte[] BinaryUserSelect(ref string err) { ClearCommand(); m_commandStringBuilder.Append("SELECT * FROM t_Users ;"); 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> /// Get user data with Binary format /// </summary> /// <returns></returns> public DataSet GetBinaryUserData() { string err = ""; byte[] bUserData = svc.ByteArrayUserSelect(ref err); if (err != "") { MessageBox.Show(err); 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; } |
1using System; 2using System.IO; 3using System.Data; 4using System.Runtime.Serialization; 5using System.Runtime.Serialization.Formatters.Binary; 6 7namespace Common 8{ 9 public class DataFormatter 10 { 11 private DataFormatter() { } 12 /**//// <summary> 13 /// Serialize the Data of dataSet to binary format 14 /// </summary> 15 /// <param name="dsOriginal"></param> 16 /// <returns></returns> 17 static public byte[] GetBinaryFormatData(DataSet dsOriginal) 18 { 19 byte[] binaryDataResult = null; 20 MemoryStream memStream = new MemoryStream(); 21 IFormatter brFormatter = new BinaryFormatter(); 22 dsOriginal.RemotingFormat = SerializationFormat.Binary; 23 24 brFormatter.Serialize(memStream, dsOriginal); 25 binaryDataResult = memStream.ToArray(); 26 memStream.Close(); 27 memStream.Dispose(); 28 return binaryDataResult; 29 } 30 /**//// <summary> 31 /// Retrieve dataSet from data of binary format 32 /// </summary> 33 /// <param name="binaryData"></param> 34 /// <returns></returns> 35 static public DataSet RetrieveDataSet(byte[] binaryData) 36 { 37 DataSet dataSetResult = null; 38 MemoryStream memStream = new MemoryStream(binaryData); 39 IFormatter brFormatter = new BinaryFormatter(); 40 41 object obj = brFormatter.Deserialize(memStream); 42 dataSetResult = (DataSet)obj; 43 return dataSetResult; 44 } 45 } 46} 47 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者