原出处不晓得是哪里,也不知道作者是谁,网上都是复制来复制去,无从考究。。 但是代码还是好用。
Rem 序列化类对象为二进制流
Public Function ObjectToBinary(ByVal KeyName As String, Obj As Object) As Byte()
Dim Byt() As Byte
Dim objBag As New PropertyBag
If Obj Is Nothing Or Len(KeyName) = 0 Then Exit Function
objBag.WriteProperty KeyName, Obj
Byt = objBag.Contents
ObjectToBinary = Byt
End Function
Rem 从二进制流加载对象
Public Function BinaryToObject(ByVal KeyName As String, Byt() As Byte) As Object
Dim oP As New PropertyBag
If Len(KeyName) = 0 Then Exit Function
oP.Contents = Byt
Set BinaryToObject = oP.ReadProperty(KeyName)
End Function
Rem 记录集对象序列化为二进制流
Rem 传入的记录集游标需要为 CursorLocation = adUseClient 否则 rs.Save s, adPersistADTG 会报错
Public Function RecordsetToBinary(rs As ADODB.Recordset) As Byte()
Dim s As New ADODB.Stream
s.Open
s.Type = adTypeBinary
rs.Save s, adPersistADTG
RecordsetToBinary = s.Read()
Set s = Nothing
End Function
Rem 二进制流转换为记录集
Public Function BinaryToRecordset(Byt() As Byte) As ADODB.Recordset
Dim s As New ADODB.Stream
Dim rs As New ADODB.Recordset
s.Open
s.Type = adTypeBinary
s.Write Byt
s.Position = 0
rs.Open s
Set BinaryToRecordset = rs
End Function
Views: 81
很好的代码,感谢分享。
示例用到了属性袋来装一些可以静态化的对象,
以及 rs 对象自带的 save/read 函数实现记录集的持久化,
方便进行远程网络传输需求的包装和还原操作,非常实用。
温馨提示,建议编辑一下文章,
选中这些代码,然后在工具栏点一下“代码”按钮,
就可以把代码进行格式化,高亮语法,以及行号显示。
阅读的时候会有个复制按钮,方便一键复制到剪贴板使用。