将byte []数组转换为DataTable

将byte []数组转换为DataTable,第1张

将byte []数组转换为DataTable

您在说的是二进制序列化和反序列化。也许这会有所帮助。

using System;using System.IO;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.Data;using System.Text;namespace Serial{    public class Ser    {        public static byte[] StrToByteArray(string str)        { UTF8Encoding  encoding = new UTF8Encoding (); return encoding.GetBytes(str);        }        public static string ByteArrayToStr(byte[] barr)        { UTF8Encoding  encoding = new UTF8Encoding (); return encoding.GetString(barr, 0, barr.Length);        }        public static void Main(String[] args)        { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32))); dt.Columns.Add(new DataColumn("StringValue", typeof(string))); dt.Columns.Add(new DataColumn("DateTimevalue", typeof(DateTime))); dt.Columns.Add(new DataColumn("BooleanValue", typeof(bool))); for (int i = 1; i <= 1; i++)  {     dr = dt.NewRow();     dr[0] = i;     dr[1] = "Item " + i.ToString();     dr[2] = DateTime.Now;     dr[3] = (i % 2 != 0) ? true : false;     dt.Rows.Add(dr); } //Serialize BinaryFormatter bformatter = new BinaryFormatter(); MemoryStream  stream = new MemoryStream(); string s; bformatter.Serialize(stream, dt); byte[] b = stream.ToArray(); s = ByteArrayToStr(b); stream.Close(); dt = null; //Now deserialise bformatter = new BinaryFormatter(); byte[] d; d = StrToByteArray(s); stream = new MemoryStream(d); dt = (DataTable)bformatter.Deserialize(stream); stream.Close();        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5431759.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存