你所在位置:首页C#.net开发 → c#两种方式 dataset 与byte[] 字节的转换函数

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

发布时间:2014-09-23 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;
                }
            
        }


上一篇:c# MD5加密 DES加密解密字符串函数
下一篇:C#注册表操作类 实用操作函数类