c#两种方式 dataset 与byte[] 字节的转换

sing System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary; 

   

        /// <summary>
       /// dataset 转转换byte[] 字节
       /// </summary>
       /// <param name="str"></param>
       public static byte[] DataToByte(DataSet ds)
        {
            byte[] bArrayResult = null;
            ds.RemotingFormat = SerializationFormat.Binary;
            MemoryStream ms = new MemoryStream();
            IFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, ds);
            bArrayResult = ms.ToArray();
            ms.Close();
            ms.Dispose();
            return bArrayResult;
        } 
       /// <summary>
       /// byte[] 字节转datasheet
       /// </summary>
       /// <param name="bArrayResult"></param>
       /// <returns></returns>
        public static DataSet ByteToDataset(byte[] bArrayResult)
           {
               DataSet dsResult = new DataSet();
               MemoryStream ms = new MemoryStream(bArrayResult);
               IFormatter bf = new BinaryFormatter();
               object obj = bf.Deserialize(ms);
               dsResult = (DataSet)obj;
               ms.Close();
               ms.Dispose(); 

           return dsResult;
               
            } 
       /// <summary>
       /// 第二种方法转换
       /// </summary>
       /// <param name="ds"></param>
       /// <returns></returns>
       public static byte[] GetBinaryFormatDataSet(DataSet ds)
        {
             //创建内存流
             MemoryStream memStream = new MemoryStream();
             //产生二进制序列化格式
             IFormatter formatter = new BinaryFormatter();
             //指定DataSet串行化格式是二进制
             ds.RemotingFormat = SerializationFormat.Binary;
             //串行化到内存中
             formatter.Serialize(memStream, ds);
             //将DataSet转化成byte[]
             byte[] binaryResult = memStream.ToArray();
             //清空和释放内存流
             memStream.Close();
             memStream.Dispose();
             return binaryResult;
         }
       /// <summary>
       ///  第二种方法转换
       /// </summary>
       /// <param name="binaryData"></param>
       /// <returns></returns>
        public static DataSet RetrieveDataSet(byte[] binaryData)
        {
              //创建内存流
                MemoryStream memStream = new MemoryStream(binaryData);
                memStream.Seek(0, SeekOrigin.Begin);
                //产生二进制序列化格式
                IFormatter formatter = new BinaryFormatter();
                //反串行化到内存中
                object obj = formatter.Deserialize(memStream);
                //类型检验
                if (obj is DataSet)
                {
                    DataSet dataSetResult = (DataSet)obj;
                    return dataSetResult;
                }
                else
                {
                    return null;
                }
            
        }


发布了2 篇原创文章 · 获赞 6 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览