如何在SQL server中存取图片?

wangtse 2003-04-06 10:55:47
VB6.0,SQL server7.
其它文本类的字段存取无问题,图片存入是用的:Rst1.Fields("Pic").AppendChunk Image1.Picture
结果提示“变量或类型不正确,或者不在可接受的范围内,要不就是与其他数据冲突”,指向语句:rst1("Pic").AppendChunk image1.Picture,(字段pic的类型设为image),请问是为什么?
...全文
30 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsgzsgzsg 2003-04-09
  • 打赏
  • 举报
回复
先将Image1.Picture存为文件,如c:\t.jpg,然后转换成长二进制数据bit,最后AppendChunk至Pic字段。
SavePicture Image1, "c:\t.jpg"
Dim bit() As Byte, fn As Byte
fn = FreeFile()
Open ("c:\t.jpg") For Binary As fn
ReDim bit(LOF(1)) As Byte
Get fn, 1, bit
Close fn
Rst1("Pic").AppendChunk bit

Rst1.Update

'取出图片,形成文件t.jpg,然后load至Image1控件
Dim fn As Byte, Chunk() As Byte
If Not IsNull(Rst1("Pic")) Then
'Chunk() = Rst1("Pic").GetChunk(Rst1("Pic").ActualSize) 'SQL Server
Chunk() = Rst1("Pic").GetChunk(0,Rst1("Pic").FieldSize) 'Access
End If
If Dir("c:\t.jpg") <> "" Then Kill "c:\t.jpg"
fn = FreeFile
Open "c:\t.jpg" For Binary Access Write As fn
Put fn, , Chunk()
Close fn

Image1.Picture = LoadPicture("c:\t.jpg")
wangtse 2003-04-08
  • 打赏
  • 举报
回复
你这两个例子我见过几次了,可惜我的水平太差,看不太明白,你能针对我的问题讲具体点吗?
yoki 2003-04-07
  • 打赏
  • 举报
回复
给2个vb的例子给你
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim stm As ADODB.Stream

Private Sub SavePictureToDB(cn As ADODB.Connection)
'将BMP图片存入数据库
On Error GoTo EH
Set stm = New ADODB.Stream
rs.Open "select ImagePath,ImageValue from tbl_Image", cn, adOpenKeyset, adLockOptimistic
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.FileName

With stm
.Type = adTypeBinary
.Open
.LoadFromFile CommonDialog1.FileName
End With
With rs
.AddNew
.Fields("ImagePath") = Text1.Text
.Fields("ImageValue") = stm.Read
.Update
End With
rs.Close
Set rs = Nothing
Exit Sub
EH: MsgBox Err.Description, vbInformation, "Error"
End Sub


Private Sub LoadPictureFromDB(cn As ADODB.Connection)
'载数据库中读出BMP图片
On Error GoTo EH
Dim strTemp As String
Set stm = New ADODB.Stream
strTemp = "c:\temp.tmp" '临时文件,用来保存读出的图片
rs.Open "select ImagePath,ImageValue from tbl_image", cn, , , adCmdText
With stm
.Type = adTypeBinary
.Open
.Write rs("ImageValue")
.SaveToFile strTemp, adSaveCreateOverWrite
.Close
End With
Image1.Picture = LoadPicture(strTemp)
Set stm = Nothing
rs.Close
Set rs = Nothing
Exit Sub
EH: MsgBox Err.Description, vbInformation, "Error"
End Sub


image类型
用picture显示
'以下两个函数是从数据库中读出图片的核心程序

Public Function GetImage(Optional Filename As String) As Variant
On Error GoTo ProcErr

Dim objRS As adodb.Recordset
Dim strSQL As String
Dim Chunk() As Byte

Set objRS = New adodb.Recordset

'strSQL = "select thumb from tblpictures where idpict='" & tblID(ThumbIndex) & "'"
strSQL = "select thumb from tblpictures where idpict= " & thumb
'strSQL = "select thumb from tblpictures where idpict='387'"
'db.Execute strSQL
objRS.Open strSQL, db, adOpenForwardOnly, adLockReadOnly

If objRS.BOF And objRS.EOF Then
GetImage = 0
GoTo ProcExit
ElseIf IsNull(objRS.Fields(0)) Then
'ErrNumber = 1001
'ErrDesc = "字段为空"
GoTo ProcExit
End If

Chunk() = objRS.Fields(0).GetChunk(objRS.Fields(0).ActualSize)
Set GetImage = Chunk2Image(Chunk(), Filename)

ProcExit:
On Error Resume Next
'objRS.Close
' Chunk() = objRS.Fields(0).GetChunk(0)
Set GetImage = Chunk2Image(Chunk(), Filename)
' Set objRS = Nothing

Exit Function

ProcErr:
GetImage = 0
Resume ProcExit
End Function


Private Function Chunk2Image(Chunk() As Byte, Optional Filename As String) As Variant
On Error GoTo ProcErr
Dim KeepFile As Boolean
Dim Datafile As Integer

KeepFile = True
If Trim(Filename) = "" Then
Filename = "c:\tmpxxdb.fil"
KeepFile = False
End If

Datafile = FreeFile
Open Filename For Binary Access Write As Datafile
Put Datafile, , Chunk()
Close Datafile

ProcExit:
Set Chunk2Image = LoadPicture(Filename)
On Error Resume Next
' If Not KeepFile Then Kill filename
Exit Function

ProcErr:
On Error Resume Next
Kill Filename
Chunk2Image = 0
End Function


34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧