Mutual conversion between Datatbel and string

Source: Internet
Author: User

Today, when making a demand, you need to transform the DataTable into string, which is a reversible conversion between the two

Datatable to String

public static string datatabletostring (DataTable dt)
{
//!@&,#$%,^&* as a concatenation word for a field String
//To prevent the connection string from being present in the DataTable data, deliberately write the concatenation string as a special character!
StringBuilder strdata = new StringBuilder ();
StringWriter sw = new StringWriter (); The current data structure of the
//datatable writes to the specified stream in XML schema
dt. WriteXmlSchema (SW);
Strdata.append (SW. ToString ());
SW. Close ();
Strdata.append ("@&@");
for (int i = 0; i < dt. rows.count;i++)//Traverse DT's line
{
DataRow row = dt. Rows[i];                     
if (i > 0)//starts with the second row of data, plus the connection string for the row
{
Strdata.append ("#$%");
}
for (int j = 0; j < dt. Columns.count;     J + +)//The column that traverses row
{
if (J > 0)                            Starting with the second field, add the field's connection string
{
Strdata.append (" ^&* ");                 
}
Strdata.append (convert.tostring (ROW[J));//Fetch Data
        
}
return strdata.tostring ();
}

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.