Conversion between Bel and string

Source: Internet
Author: User

Datatable to string


Public static string DataTableToString (DataTable dt)
{
//! @ &, # $ %, ^ & * Is the concatenation string of the field
// To prevent the connection string from being present in the able data, write the concatenated string as a special character!
StringBuilder strData = new StringBuilder ();
StringWriter sw = new StringWriter ();

// The current data structure of the DataTable is written to the specified stream in XML format
Dt. WriteXmlSchema (sw );
StrData. Append (sw. ToString ());
Sw. Close ();
StrData. Append ("@&@");
For (int I = 0; I <dt. Rows. Count; I ++) // traverses dt Rows.
{
DataRow row = dt. Rows [I];
If (I> 0) // starts from the second row of data and adds the connection string of the row
{
StrData. Append ("# $ % ");
}
For (int j = 0; j <dt. Columns. Count; j ++) // traverses the row column
{
If (j> 0) // starts from the second field and adds the connection string of the field
{
StrData. Append ("^ &*");
}
StrData. Append (Convert. ToString (row [j]); // retrieves data
}
}

Return strData. ToString ();
}

 

String to Datatable


Public static DataTable StringToDataTable (string strdata)
{
If (string. IsNullOrEmpty (strdata ))
{
Return null;
}
DataTable dt = new DataTable ();
String [] strSplit = {"@&@"};
String [] strRow = {"# $ %"}; // string of the decomposed row
String [] strColumn = {"^ & *"}; // string of the decomposed Field

String [] strArr = strdata. Split (strSplit, StringSplitOptions. None );
StringReader sr = new StringReader (strArr [0]);
Dt. ReadXmlSchema (sr );
Sr. Close ();


String strTable = strArr [1]; // retrieves table data
If (! String. IsNullOrEmpty (strTable ))
{
String [] strRows = strTable. Split (strRow, StringSplitOptions. None); // parse the string array in a row
For (int rowIndex = 0; rowIndex <strRows. Length; rowIndex ++) // traverses the string array of the row
{
String vsRow = strRows [rowIndex]; // returns the string of the row.
String [] vsColumns = vsRow. Split (strColumn, StringSplitOptions. None); // parse it into a field array
Dt. Rows. Add (vsColumns );
}
}
Return dt;
}

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.