中国农业建设信息网站,网页qq登录保护在哪里,青岛李沧区网站建设,株洲市天元区建设局网站在将自定义对象或者数组等这样的数据存储到数据库时往往需要转换成二进制字节#xff0c;尤其是在一些O/RM数据库框架中#xff0c;下面是转换的函数#xff0c;一个是将对象转换成二进制字节数组#xff0c;另一个是将从数据库中读取的二进制流转换成程序中的对象。
这里…在将自定义对象或者数组等这样的数据存储到数据库时往往需要转换成二进制字节尤其是在一些O/RM数据库框架中下面是转换的函数一个是将对象转换成二进制字节数组另一个是将从数据库中读取的二进制流转换成程序中的对象。
这里参考的资源来源
C#序列化数据对象。它是将对象先转换成字节数组然后再转换成Base64字符串了字符串也可以存储到数据库中但是不如直接将字节数组转换成Blob数据库对象简单 using System; using System.Collections.Generic; using System.Linq; using System.Text; //引入三个命名空间 using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace Logistics { public class SerializeObjectToString { //将Object类型对象(注必须是可序列化的对象)转换为二进制序列字符串 public string SerializeObject(object obj) { IFormatter formatter new BinaryFormatter(); string result string.Empty; using (MemoryStream stream new MemoryStream()) { formatter.Serialize(stream, obj); byte[] byt new byte[stream.Length]; byt stream.ToArray(); //result Encoding.UTF8.GetString(byt, 0, byt.Length); result Convert.ToBase64String(byt); stream.Flush(); } return result; } //将二进制序列字符串转换为Object类型对象 public object DeserializeObject(string str) { IFormatter formatter new BinaryFormatter(); //byte[] byt Encoding.UTF8.GetBytes(str); byte[] byt Convert.FromBase64String(str); object obj null; using (Stream stream new MemoryStream(byt, 0, byt.Length)) { obj formatter.Deserialize(stream); } return obj; } } } 下面是直接转换成字节的函数 using System; using System.Collections.Generic; using System.Linq; using System.Text; //引入三个命名空间 using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace Logistics { public class SerializeObjectToString { //将Object类型对象(注必须是可序列化的对象)转换为二进制序列字符串 public byte[] SerializeObject(object obj) { IFormatter formatter new BinaryFormatter(); byte[] result null; using (MemoryStream stream new MemoryStream()) { formatter.Serialize(stream, obj); byte[] byt new byte[stream.Length]; byt stream.ToArray(); resultbyt; stream.Flush(); } return result; } //将二进制序列字符串转换为Object类型对象 public object DeserializeObject(byte[] bt) { IFormatter formatter new BinaryFormatter(); object obj null; using (Stream stream new MemoryStream(bt, 0, bt.Length)) { obj formatter.Deserialize(stream); } return obj; } } } 文章转载自: http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn http://www.morning.fgxr.cn.gov.cn.fgxr.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.fglyb.cn.gov.cn.fglyb.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.prqdr.cn.gov.cn.prqdr.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.gnyhc.cn.gov.cn.gnyhc.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.trsfm.cn.gov.cn.trsfm.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.rdkt.cn.gov.cn.rdkt.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn