图片放到SQL Server的数据库中

panxuejian 2003-03-27 10:21:28
在程序中这样将JPG图片存放到SQL Server的数据库中,急用!在线等待!
...全文
42 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
  • 打赏
  • 举报
回复
查一下以前的记录,多得不得了。

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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