AE中读写Blob字段

在之前,我都尽量避免读写blob字段,原因如下
第一:不方便手工维护。
第二:不方便做数据转换。
第三:其他人不知道里面放了什么,对于交流上有障碍。
最近,由于项目的需要,不得不去实现blob字段的读写。
AE的对象都是存在数据库中里的blob字段的,他自己实现了对象的流化(序列化),那么他一定也提供了接口出来。大不了,用API去读写。这是我最初的想法。
这其中探索的过程就不多说了,下面用代码来说明
        void SaveHistoryToFld(IFeature pFea, THistorys pHistorys)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream Ms = new MemoryStream();
            formatter.Serialize(Ms, pHistorys);//对象序列化
            int n = (int)Ms.Length;
            Ms.Position = 0;
            byte[] pReadByte = new Byte[n];
            Ms.Read(pReadByte, 0, n);//读取到内存中
            IMemoryBlobStream2 pBlob = new MemoryBlobStreamClass();
            pBlob.ImportFromMemory(ref pReadByte[0], (uint)n);//转到AE认可的接口中
            SetFldValue(pFea, CC_History, pBlob);//保存
        }
        bool LoadHistoryFromFld(IFeature pFea, out THistorys pHistorys)
        {
            pHistorys = null;
            object pObj = GetFldValue(pFea, CC_History);//读取blob字段
            if (pObj == null || pObj == DBNull.Value) return false;
            IMemoryBlobStream2 pBlob = new MemoryBlobStreamClass();
            pBlob = (IMemoryBlobStream2)pObj;//转换到AE认可的内存流中
            int n = (int)pBlob.Size;
            byte[] pReadByte = new Byte[n];
            object pObj2 = null;
            (pBlob as IMemoryBlobStreamVariant).ExportToVariant(out pObj2);//输出到变量
            pReadByte = (byte[])pObj2;//强制转换为字节数组
            Stream Ms = new MemoryStream();
            Ms.Write(pReadByte, 0, n);//强制转换为内存流
            Ms.Position = 0;
            IFormatter formatter = new BinaryFormatter();
            pHistorys = (THistorys)formatter.Deserialize(Ms);//反序列化
            return true;
        }
代码如此清楚,我就不多说了。^_^
原文地址:https://www.cnblogs.com/feedback/p/1208275.html