我本来想用一个字段保存图片存放路径和文件名的。可是由于要在网络中应用,找不到文件。还有什么更好的办法吗,请大家帮忙!!
解决方案 »
- 怎样动态添加treeview节点呀?
- 高分求救 delphi中的Pchar型 和vb中那个参数 相同
- 急需vsview6高分相赠。
- 很简单的问题.
- 救命!
- 关于mshflexgrid控件的 背景色改变后又变回去的问题(急,在线等!)
- 请问,是否有一函数?
- 立即给分! vb6编译好的文件,从资源管理器中选择该文件,看文件属性,可以知道详细版本号。怎么用代码得出磁盘上的指定VB程序的版本号?
- 高人解决了一个!可以编写这样的函数:传递一个窗体名字符串,show出一个窗体,可以传递一个函数名及参数,调用一个函数吗?
- SendMessage 的困惑!
- ActiveX控件的问题
- 怎样让窗口显示时是全屏幕?
主要解答者: w18ily 提交人: shawls
感谢: shawls、w18ily
审核者: shawls 论坛对应贴子: 查看
A : 我现在正在广州做一个考试系统,需要用VB程序将图片保存到数据库 SQL SERVER 中,并从数据库中读出该图片。
也可把代码贴在论坛中。
非常谢谢!
两个解答都是使用adodb.stream来完成的
shawls的解答
---------------------------------------------------------------
Private Sub Command3_Click()
Dim c As New ADODB.Connection
c.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False "
c.Execute "create table a (b longbinary) "
End Sub
Private Sub Command4_Click()
Set b = New ADODB.Recordset
Set c = New ADODB.Stream
c.Mode = adModeReadWrite
c.Type = adTypeBinary
c.Open
c.LoadFromFile "c:\1.bmp "
b.Open "select * from a ", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False ", adOpenDynamic, adLockOptimistic
b.AddNew
b.Fields.Item(0).Value = c.Read()
b.Update
b.Close
Set b = New ADODB.Recordset
b.Open "select * from a ", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False ", adOpenKeyset, adLockOptimistic
MsgBox b.RecordCount
b.MoveLast
c.Write (b.Fields.Item(0).Value)
c.SaveToFile "c:\aa.bmp ", adSaveCreateOverWrite
Picture1.Picture = LoadPicture( "c:\aa.bmp ")
End Sub
w18ly的解答
---------------------------------------------------------------
'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