怎样将数据库中的图片字段取出存为一个临时的图片文件?

kevenzhang 2001-11-13 02:57:05
...全文
93 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kevenzhang 2001-11-13
我不知道别人怎么写的呀 ,我不能都把它存为BMP吧
回复
solitude 2001-11-13
to : kevenzhang(向往高手) 
你怎么写进出的就怎么读出来
回复
kevenzhang 2001-11-13
如果我不知道数据库中是GIF,还是BMP,或JPG,TIF,甚至是EXE,我怎么还原它当初的
文件格式呢?
回复
yangzhaoyu 2001-11-13
http://support.microsoft.com/support/kb/articles/Q153/2/38.asp
回复
foolishtiger 2001-11-13
我贴的是我程序里的一段代码,把指定ID的文件,由用户通过对话框指出要保存的位置,然后把Content字段的内容(该字段的内容是图片)保存成用户指定的文件名

这段代码我几乎每天都要运行.
回复
kevenzhang 2001-11-13
我的RECORDSET对象没有ACTUALSIZE这个属性呀,告诉我怎办
回复
kevenzhang 2001-11-13
foolishtiger(吴文智):
我用的是ADO,字段是“图象”,你说怎写吧,我有点看不明?
回复
fishzone 2001-11-13
http://www.csdn.net/expert/topic/242/242007.shtm
给你贴出来吧!
'可以用临时文件的方法来储存和读出文件

'储存
Private Sub SavePic()
Dim Conn As New ADODB.Connection
Dim Rs0 As New ADODB.Recordset
Dim Rs As New ADODB.Recordset
Dim Maxnumber As Long
Dim Sql As String
Dim Connstr As String

Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=joybig;Data Source=" & App.Path & "\pic.mdb;Persist Security Info=False"
Conn.Open Connstr
Rs.CursorLocation = adUseClient
Rs.CursorType = adOpenKeyset
Rs.LockType = adLockOptimistic
DoEvents

Dim picarray() As Byte
Dim piclenth As Long
Rs.Open "save", Conn, , , adCmdTable
Rs.AddNew
Rs("title") = Trim(Text3.Text)
If Trim(Text1.Text) = "在此处输入备注信息" Then
Rs("note") = ""
Else
Rs("note") = Trim(Text1.Text)
End If
piclenth = FileLen(App.Path & "\mydoc.doc")
ReDim picarray(piclenth)
Open App.Path & "\mydoc.doc" For Binary Access Read As #1
Get #1, , picarray()
Close #1
Rs("pic").AppendChunk picarray
Rs.Update
Rs.Close
Set Rs = Nothing
Set Conn = Nothing
End Sub

'读出
Private Sub ReadPic()
Dim Conn As New ADODB.Connection
Dim Rs0 As New ADODB.Recordset
Dim Rs As New ADODB.Recordset
Dim Maxnumber As Long
Dim Sql As String
Dim Connstr As String
Dim picarray() As Byte
Dim piclenth As Long

Connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=joybig;Data Source=" & App.Path & "\pic.mdb;Persist Security Info=False"
Conn.Open Connstr
Rs.CursorLocation = adUseClient
Rs.CursorType = adOpenKeyset
Rs.LockType = adLockOptimistic
DoEvents

Rs.Open "select * from [save]", Conn, adOpenKeyset, adLockOptimistic
Text2.Text = Rs("title")
Text4.Text = Rs("note")
piclenth = Rs("pic").ActualSize

ReDim picarray(piclenth)
picarray = Rs("pic").GetChunk(piclenth)

Open App.Path & "\doc1.doc" For Binary Access Write As #1
Put #1, , picarray()
Close #1
Picture1.Picture = LoadPicture(App.Path & "\doc1.doc")
Rs.Close
Set Rs = Nothing
Set Conn = Nothing
End Sub
回复
foolishtiger 2001-11-13
从数据库取文件出来
Public Sub SaveFile(ByVal FileID As Long)
Dim lngBlockCount As Long
Dim lngLastBlock As Long
Dim lngI As Long
Dim btyBlock() As Byte
Dim lngResult As Long

If rsBinary.EOF And rsBinary.BOF Then Exit Sub
rsBinary.MoveFirst
rsBinary.Find " id=" & FileID
If Not rsBinary.EOF Then
With frmBinary.CommonDialog1
.FileName ="TempSave"
'.InitDir = App.Path

'If user cancel save the goto handle
On Error GoTo ErrorHandle
.ShowSave
If .FileName <> "" Then
lngBlockCount = rsBinary.Fields("content").ActualSize \ BlockSize
lngLastBlock = rsBinary.Fields("content").ActualSize Mod BlockSize

If Dir(.FileName) <> "" Then
If MsgBox("File " & .FileName & " is exist,overwrite?", vbYesNo + vbQuestion) = vbYes Then
Kill .FileName
Else
Exit Sub
End If
Else
End If

Open .FileName For Binary As #1

ReDim btyBinary(BlockSize)

For lngI = 1 To lngBlockCount
btyBlock() = rsBinary.Fields("content").GetChunk(BlockSize)
Put #1, , btyBlock
Next

If lngLastBlock <> 0 Then
ReDim btyBlock(lngLastBlock)
btyBlock() = rsBinary.Fields("content").GetChunk(lngLastBlock)
Put #1, , btyBlock
End If

Close #1
MsgBox .FileName & " is saved", vbInformation
Else
End If
End With
End If

Exit Sub
ErrorHandle:

End Sub


回复
相关推荐
发帖

1188

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2001-11-13 02:57
社区公告
暂无公告