AO中保存二进制大对象(BLOB)
示例代码演示如何保存一个.lyr文件
//保存
Private Sub UIButtonControl1_Click()
' Get the IPersistStream for the first layer from the map
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pPersist As IPersistStream
Set pPersist = pMxDoc.FocusMap.Layer(0)
' Now persist the layer to the memory BLOB stream
Dim pMemoryStream As IMemoryBlobStream
Set pMemoryStream = New MemoryBlobStream
pPersist.Save pMemoryStream, False
' Finally, save the BLOB into the database
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New AccessWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace =
pWorkspaceFactory.OpenFromFile("C:/Source/BLOB.mdb", 0)
Dim pTable As ITable
Set pTable = pFeatureWorkspace.OpenTable("MyLayers")
Dim pRow As IRow
Set pRow = pTable.CreateRow
pRow.Value(pRow.Fields.FindField("Layers")) = pMemoryStream
pRow.Store
End Sub
//读取
Private Sub UIButtonControl2_Click()
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New AccessWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace =
pWorkspaceFactory.OpenFromFile("C:/Source/BLOB.mdb", 0)
Dim pTable As ITable
Set pTable = pFeatureWorkspace.OpenTable("MyLayers")
Dim pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)
Dim pRow As IRow
Set pRow = pCursor.NextRow
If (pRow Is Nothing) Then Exit Sub
Dim pMemoryStream As IMemoryBlobStream
Set pMemoryStream = pRow.Value(pRow.Fields.FindField("Layers"))
Dim pLayer As ILayer
Set pLayer = New FeatureLayer
Dim pPersist As IPersistStream
Set pPersist = pLayer
pPersist.Load pMemoryStream
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
pMxDoc.FocusMap.AddLayer pLayer
End Sub
示例代码演示如何保存一个.lyr文件
//保存
Private Sub UIButtonControl1_Click()
' Get the IPersistStream for the first layer from the map
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pPersist As IPersistStream
Set pPersist = pMxDoc.FocusMap.Layer(0)
' Now persist the layer to the memory BLOB stream
Dim pMemoryStream As IMemoryBlobStream
Set pMemoryStream = New MemoryBlobStream
pPersist.Save pMemoryStream, False
' Finally, save the BLOB into the database
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New AccessWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace =
pWorkspaceFactory.OpenFromFile("C:/Source/BLOB.mdb", 0)
Dim pTable As ITable
Set pTable = pFeatureWorkspace.OpenTable("MyLayers")
Dim pRow As IRow
Set pRow = pTable.CreateRow
pRow.Value(pRow.Fields.FindField("Layers")) = pMemoryStream
pRow.Store
End Sub
//读取
Private Sub UIButtonControl2_Click()
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New AccessWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace =
pWorkspaceFactory.OpenFromFile("C:/Source/BLOB.mdb", 0)
Dim pTable As ITable
Set pTable = pFeatureWorkspace.OpenTable("MyLayers")
Dim pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)
Dim pRow As IRow
Set pRow = pCursor.NextRow
If (pRow Is Nothing) Then Exit Sub
Dim pMemoryStream As IMemoryBlobStream
Set pMemoryStream = pRow.Value(pRow.Fields.FindField("Layers"))
Dim pLayer As ILayer
Set pLayer = New FeatureLayer
Dim pPersist As IPersistStream
Set pPersist = pLayer
pPersist.Load pMemoryStream
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
pMxDoc.FocusMap.AddLayer pLayer
End Sub