如何将图片添加到数据库中?

时间:2013-01-22 11:26:51

标签: oracle vb6

我创建了一个包含一列BLOB数据类型的表mypics。

现在我需要在此表中实现vb6代码来选择/插入/更新数据,但我不知道如何处理BLOB列......

SQL> desc mypics  名字空?型


PID NOT NULL NUMBER(38)

PNAME CHAR(10)

IMAGE BLOB

请帮忙

1 个答案:

答案 0 :(得分:0)

以下是一些示例代码,可帮助您入门。假设一个名为tblImages的表有3个字段。

Field       Data Type     Size
Picture         Image          
ID              Int            4

将图像从磁盘上的文件添加到数据库

Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile strFileName

strSQL = "SELECT ID, Picture FROM tblImages"
Set rs = New ADODB.Recordset
With rs
    .ActiveConnection = cn
    .Source = strSQL 
    .CursorType = adOpenKeyset
    .LockType = adLockOptimistic
    .Open
End With

rs.AddNew   
rs.Fields("ID").Value = ID
rs.Fields("Picture").Value = strStream.Read
rs.Update
rs.Close
Set rs = Nothing

要将文件从数据库中提取到磁盘文件:

strSQL = "SELECT Picture FROM tblImages WHERE ID = " & ID               
Set rs = New ADODB.Recordset
With rs
    .ActiveConnection = cn
    .Source = strSQL
    .Open
End With

If Not (rs.BOF And rs.EOF) Then
    Set strStream = New ADODB.Stream
    strStream.Type = adTypeBinary
    strStream.Open    
    strStream.Write rs!Picture
    strStream.SaveToFile TempPath, adSaveCreateOverWrite        
    strStream.Close
    Set strStream = Nothing        
End If    
rs.Close
Set rs = Nothing

我希望这会有所帮助。