图片放到SQL Server的数据库中

panxuejian 2003-03-27 10:21:28
在程序中这样将JPG图片存放到SQL Server的数据库中,急用!在线等待!
...全文
27 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chenyu5188 2003-03-27
UP

楼上几位给的代码太详细了
回复
online 2003-03-27
Dim Chunk() As Byte
Chunk() = Image2Chunk(Filename)

.Fields("thumb").AppendChunk Chunk()
.Update


Private Function Image2Chunk(Filename As String) As Variant
On Error GoTo ProcErr
Dim Datafile As Integer
Dim FileLength As Long
Dim Chunk() As Byte

Datafile = FreeFile
Open Filename For Binary Access Read As Datafile
FileLength = LOF(Datafile)
If FileLength = 0 Then GoTo ProcErr
ReDim Chunk(FileLength)
Get Datafile, , Chunk()
Close Datafile

ProcExit:
Image2Chunk = Chunk()
Exit Function

ProcErr:
Image2Chunk = 0
End Function
回复
ClientDC 2003-03-27
存入ACCESS数据库中:
Dim rs As New ADODB.Recordset
Dim Rss As New ADODB.Stream
Dim cnn As ADODB.Connection
Dim cnstr As String
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\pic.mdb;Mode=ReadWrite;Persist Security Info=False"
Set cnn = New ADODB.Connection
cnn.Open cnstr
rs.Open "test", cnn, adOpenStatic, adLockOptimistic
Rss.Type = adTypeBinary
Rss.Open
Rss.LoadFromFile App.Path & "\1.jpg"
rs.AddNew
rs.Fields("a1") = "PICTURE"
rs.Fields("a2") = Rss.Read
rs.Update
rs.Close
cnn.Close

a2的类型为OLE


读取:
Dim rs As New ADODB.Recordset
Dim Rss As New ADODB.Stream
Dim cnn As ADODB.Connection
Dim cnstr As String
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\pic.mdb;Mode=ReadWrite;Persist Security Info=False"
Set cnn = New ADODB.Connection
cnn.Open cnstr
rs.Open "test", cnn, adOpenStatic, adLockOptimistic
Rss.Type = adTypeBinary
Rss.Open
If Not rs.EOF Then
Rss.Write rs.Fields("a2")
Rss.SaveToFile App.Path & "\tmp.jpg" ‘存为硬盘文件
Picture1.Picture = LoadPicture(App.Path & "\tmp.jpg")
Kill App.Path & "\tmp.jpg"
End If
rs.Close
cnn.Close
如果要存别的类型,用类似的方法。
回复
qqqdong 2003-03-27
'Use ADODB.Stream Method
'After ADO 2.6
'Import the Image to SQLServer
Private Sub ImportBLOB(cn As ADODB.Connection)

Dim rs As New ADODB.Recordset
Dim stm As ADODB.Stream

Set stm = New ADODB.Stream

' Skip any table not found errors
On Error Resume Next
cn.Execute "drop table BinaryObject"

On Error GoTo 0
'Create the BinaryObject table
cn.Execute "create table BinaryObject " & _
"(blob_id int IDENTITY(1,1), " & _
"blob_filename varchar(256), " & _
"blob_object image)"

rs.Open "Select * from BinaryObject where 1=2", cn, adOpenKeyset, adLockOptimistic
'Read the binary files from disk
stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile App.Path & "\BLOBsample.jpg"

rs.AddNew
rs!blob_filename = App.Path & "\BLOBsample.jpg"
rs!blob_object = stm.Read

'Insert the binary object in the table
rs.Update

rs.Close
stm.Close

Set rs = Nothing
Set stm = Nothing

End Sub
'Display the image on image control
Private Sub DisplayBLOB(cn As ADODB.Connection)

Dim rs As New ADODB.Recordset

' Select the only image in the table
rs.Open "Select * from BinaryObject where blob_id = 1", cn

' Set the DataSource to the recordset
Set imgBinaryData.DataSource = rs
'Set the DataField to the BLOB field
imgBinaryData.DataField = rs!blob_object.Name

'Release the recordset
rs.Close
Set rs = Nothing

End Sub
回复
panxuejian 2003-03-27
不明白?
回复
abc10 2003-03-27
查一下以前的记录,多得不得了。
回复
相关推荐
发帖

1187

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2003-03-27 10:21
社区公告
暂无公告