Datatable serialization and deserialization

Source: Internet
Author: User

// Able serialization

Public string getSendDetailQuery (DateTime timeS, DateTime timeE, string sccount)
{
Try
{
SmsOperate so = new SmsOperate ();

// Obtain dt
DataTable dtt = so. getSendDetailQuery (timeS, timeE, sccount );
JavaScriptSerializer serializer = new JavaScriptSerializer ();
String aaa = Serialize (dtt, false); // datatable cannot be serialized directly. This is a serialization method.
Return "0:" + aaa;

}
Catch (Exception e)
{
Return "-1" + e. Message;
}
}

/// <Summary> serialization Method
/// No paging is required
/// </Summary>
/// <Param name = "dt"> </param>
/// <Param name = "flag"> false </param>
/// <Returns> </returns>
Public string Serialize (DataTable dt, bool flag)
{
JavaScriptSerializer serializer = new JavaScriptSerializer ();
List <Dictionary <string, object> list = new List <Dictionary <string, object> ();
Foreach (DataRow dr in dt. Rows)
{
Dictionary <string, object> result = new Dictionary <string, object> ();
Foreach (DataColumn dc in dt. Columns)
{
Result. Add (dc. ColumnName, dr [dc]. ToString ());
}
List. Add (result );
}
Return serializer. Serialize (list );;
}

 

// Deserialize www.2cto.com

Public DataTable getSendDetailTest ()
{
DataTable dtb = new DataTable ();

// Get the serialization result aaa
String aaa = getSendDetailQuery (Convert. ToDateTime ("00:00:00"), Convert. ToDateTime ("23:59:59"), "wangsub1 ");

If (aaa. Substring (0, 1) = "0 ")
{
Try
{
JavaScriptSerializer serializer = new JavaScriptSerializer ();
// Var obj = serializer. DeserializeObject (aaa); // deserialization

ArrayList dic = serializer. Deserialize <ArrayList> (aaa); // Deserialize the ArrayList type

If (dic. Count> 0)
{
Foreach (Dictionary <string, object> drow in dic)
{
If (dtb. Columns. Count = 0)
{
Foreach (string key in drow. Keys)
{
Dtb. Columns. Add (key, drow [key]. GetType (); // Add the dt column name
}
}
DataRow row = dtb. NewRow ();
Foreach (string key in drow. Keys)
{

Row [key] = drow [key]; // Add a column Value
}
Dtb. Rows. Add (row); // Add a row
}
}
}
Catch (Exception e)
{
//
}
}
Else
{
//
}

Return dtb;
}


From SYZ_YUMEIZHOU_YY

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.