C# DataSet对象序列化并压缩

3/25/2009来源:C#应用人气:9770

今天做一个项目中用到把DataSet对象序列化成文件,并上传到服务器,不过生成的文件比较大,就想着压缩一下,找了一下资料,用GZipStream来压缩了一下,效果还是非常理想的。现在把这部分代码整理了一下:

 

view plaincopy to clipboardPRint?
using System.Data;  
using System.IO;  
using System.IO.Compression;  
using System.Runtime.Serialization;  
using System.Runtime.Serialization.Formatters.Binary;  
 
namespace DataSetSerializerDeserialize  
{  
    class Program  
    {  
        /// <summary>  
        /// 序列化DataSet对象并压缩  
        /// </summary>  
        /// <param name="ds"></param>  
        static void DataSetSerializerCompression(DataSet ds)  
        {  
            IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象  
              
            MemoryStream ms = new MemoryStream();//创建内存流对象  
 
            formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流  
 
            byte[] buffer = ms.ToArray();//把内存流对象写入字节数组  
 
            ms.Close();//关闭内存流对象  
 
            ms.Dispose();//释放资源  
 
            FileStream fs = File.Create("datasetCompression.dat");//创建文件  
 
            GZipStream gzipStream = new GZipStream(fs, CompressionMode.Compress, true);//创建压缩对象  
 
            gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件  
 
            fs.Close();//关闭流  
 
            fs.Dispose();//释放对象  
        }  
 
        /// <summary>  
        /// 不压缩直接序列化DataSet  
        /// </summary>  
        /// <param name="ds"></param>  
        static void DataSetSerializer(DataSet ds)  
        {  
            IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象  
 
            FileStream fs = File.Create("dataset.dat");//创建文件  
 
            formatter.Serialize(fs, ds);//把DataSet对象序列化到文件  
 
            fs.Close();//关闭流  
 
            fs.Dispose();//释放对象  
        }  
 
        static void Main(string[] args)  
        {  
            DataTable table = new DataTable("ParentTable");  
              
            DataColumn column;  
            DataRow row;  
 
            column = new DataColumn();  
            column.DataType = System.Type.GetType("System.Int32");  
            column.ColumnName = "id";  
            column.ReadOnly = true;  
            column.Unique = true;  
            table.Columns.Add(column);  
 
            column = new DataColumn();  
            column.DataType = System.Type.GetType("System.String");  
            column.ColumnName = "ParentItem";  
            column.AutoIncrement = false;  
            column.Caption = "ParentItem";  
            column.ReadOnly = false;  
            column.Unique = false;  
            table.Columns.Add(column);  
 
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];  
            PrimaryKeyColumns[0] = table.Columns["id"];  
            table.PrimaryKey = PrimaryKeyColumns;  
 
            DataSet dataSet = new DataSet();  
            dataSet.Tables.Add(table);  
 
            for (int i = 0; i <= 100; i++)  
            {  
                row = table.NewRow();  
                row["id"] = i;  
                row["ParentItem"] = "ParentItem " + i;  
                table.Rows.Add(row);  
            }  
            DataSetSerializer(dataSet);  
            DataSetSerializerCompression(dataSet);  
        }  
    }  

using System.Data;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace DataSetSerializerDeserialize
{
    class Program
    {
        /// <summary>
        /// 序列化DataSet对象并压缩
        /// </summary>
        /// <param name="ds"></param>
        static void DataSetSerializerCompression(DataSet ds)
        {
            IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
           
            MemoryStream ms = new MemoryStream();//创建内存流对象

            formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流

            byte[] buffer = ms.ToArray();//把内存流对象写入字节数组

            ms.Close();//关闭内存流对象

            ms.Dispose();//释放资源

            FileStream fs = File.Create("datasetCompression.dat");//创建文件

            GZipStream gzipStream = new GZipStream(fs, CompressionMode.Compress, true);//创建压缩对象

            gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件

            fs.Close();//关闭流

            fs.Dispose();//释放对象
        }

        /// <summary>
        /// 不压缩直接序列化DataSet
        /// </summary>
        /// <param name="ds"></param>
        static void DataSetSerializer(DataSet ds)
        {
            IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象

            FileStream fs = File.Create("dataset.dat");//创建文件

            formatter.Serialize(fs, ds);//把DataSet对象序列化到文件

            fs.Close();//关闭流

            fs.Dispose();//释放对象
        }

        static void Main(string[] args)
        {
            DataTable table = new DataTable("ParentTable");
           
            DataColumn column;
            DataRow row;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "id";
            column.ReadOnly = true;
            column.Unique = true;
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "ParentItem";
            column.AutoIncrement = false;
            column.Caption = "ParentItem";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);

            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["id"];
            table.PrimaryKey = PrimaryKeyColumns;

            DataSet dataSet = new DataSet();
            dataSet.Tables.Add(table);

            for (int i = 0; i <= 100; i++)
            {
                row = table.NewRow();
                row["id"] = i;
                row["ParentItem"] = "ParentItem " + i;
                table.Rows.Add(row);
            }
            DataSetSerializer(dataSet);
            DataSetSerializerCompression(dataSet);
        }
    }
}
 

 

这里先把序列化及压缩的部分代码贴出来,明天贴解压及反序列化的代码。

http://blog.csdn.net/wlkjhxd/archive/2009/03/24/4021436.aspx