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 ();
}