How to convert DataTable to Base64?

  • Thread starter Thread starter Rodusa
  • Start date Start date
R

Rodusa

I am looking for an example of how to save a DataTable into Base64 and
vice-versa. I tried using Convert.ToBase64String(), but it says that it
cannot convert DataTable to byte[]. Any help or pointing would be
appreciated.

Thanks

Rod
 
Rodusa,

The easiest way to do this would be to get the contents of the DataTable
in XML, then convert that to a byte array, then convert that to the base 64
string. To get the XML, use the WriteXml method on the DataTable (this
might be new for 2.0, I can't remember, if so, then use the WriteXml method
on the DataSet that the DataTable is in). Once you have the XML, you can
convert to a byte array using one of the Encoding classes (use
UnicodeEncoding if you don't know the encodings of the XML, or another more
appropriate one if you know the encoding will always be the same). Once you
have the bytes from the encoding, getting the base 64 string is easy.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top