C # DataSet object serialization and compression

Source: Internet
Author: User
Tags object serialization serialization

Today to do a project used to serialize dataset objects into a file, and upload to the server, but the resulting file is relatively large, think of compression, looked for information, with GZipStream to compress a bit, the effect is very ideal. Now sort this part of the code:

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>
Serializes the DataSet object and compresses
</summary>
<param name= "DS" ></param>
static void Datasetserializercompression (DataSet ds)
{
IFormatter formatter = new BinaryFormatter ();//define BinaryFormatter to serialize the DataSet object

MemoryStream ms = new MemoryStream ();//create memory Stream object

Formatter. Serialize (MS, DS);//serialization of DataSet objects to memory stream

byte[] buffer = Ms. ToArray ()//writes a memory stream object to a byte array

Ms. Close ();//Shut down memory stream object

Ms. Dispose ();/release Resources

FileStream fs = File.create ("DatasetCompression.dat");//Create File

GZipStream GZipStream = new GZipStream (FS, compressionmode.compress, True);//Create a compressed object

Gzipstream.write (buffer, 0, buffer. LENGTH);//write compressed data to file

Gzipstream.close ();//Turn off the compression stream, here to note: Be sure to close, otherwise the decompression will appear less than 4K file read data, more than 4K of the file read incomplete

Gzipstream.dispose ()//Release Object

Fs. Close ();//Off stream

Fs. Dispose ();/Release Object
}

<summary>
Do not compress a direct serialized dataset
</summary>
<param name= "DS" ></param>
static void Datasetserializer (DataSet ds)
{
IFormatter formatter = new BinaryFormatter ();//define BinaryFormatter to serialize the DataSet object

FileStream fs = File.create ("Dataset.dat");//Create File

Formatter. Serialize (FS, DS);//serialization of DataSet objects to a file

Fs. Close ();//Off stream

Fs. Dispose ();/Release Object
}

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 <= i++)
{
row = table. NewRow ();
row["id"] = i;
row["ParentItem"] = "parentitem" + i;
Table. Rows.Add (row);
}
Datasetserializer (DataSet);
Datasetserializercompression (DataSet);
}
}
}

Here we will post the serialized and compressed parts of the code, and post the uncompressed and deserialized code tomorrow.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.