VB中如何将图片保存到SQLSERVER数据库中?解决后立刻结帖

cshmai 2006-04-29 04:51:42
如题,刚接触VB,请大家说的详细点。谢谢了
...全文
293 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
province_ 2006-04-29
  • 打赏
  • 举报
回复
你是做ASP,那就没必要把图象内容存到库里去了,只要把图片路径存到库里就可以了。这样数据库尺寸不会太大,速度快。
zhuaiman 2006-04-29
  • 打赏
  • 举报
回复
使用流对象保存和显示图片
打开vb6,新建工程。

添加两个按钮,一个image控件
注意:Access中的photo字段类型为OLE对象.
SqlServer中的photo字段类型为Image

'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
‘2.5版本以下不支持Stream对象
Dim iConcstr As String
Dim iConc As ADODB.Connection


'保存文件到数据库中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcstr As String

'读取文件到内容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile App.Path + "\test.jpg"
End With


'打开保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "select * from img", iConc, 1, 3
.AddNew '新增一条记录
.Fields("photo") = iStm.Read
.Update
End With


'完成后关闭对象
iRe.Close
iStm.Close
End Sub


Sub s_ReadFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
'打开表
Set iRe = New ADODB.Recordset
‘得到最新添加的纪录
iRe.Open "select top 1 * from img order by id desc", iConc, adOpenKeyset, adLockReadOnly
'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("photo")
‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
.SaveToFile App.Path & "\test1.jpg"
End With


Image1.Picture = LoadPicture(App.Path & "\test1.jpg")
'关闭对象
iRe.Close
iStm.Close
End Sub


Private Sub Command1_Click()
Call s_ReadFile
End Sub


Private Sub Command2_Click()
Call s_SaveFile
End Sub


Private Sub Form_Load()
'数据库连接字符串
iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=F:\csdn_vb\database\保存图片\access图片\img.mdb"

‘下面的语句是连接sqlserver数据库的.
‘iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
‘ "User ID=sa;Password=;Initial Catalog=test;Data Source=yang"


Set iConc = New ADODB.Connection
iConc.Open iConcstr
End Sub


Private Sub Form_Unload(Cancel As Integer)
iConc.Close
Set iConc = Nothing
End Sub
cshmai 2006-04-29
  • 打赏
  • 举报
回复
我是做ASP.NET的,最近公司让我做VB,可是一点都不会呀,连最基础的代码看了都有点晕,呵呵
能在详细一点吗
x_x_j 2006-04-29
  • 打赏
  • 举报
回复
读出来时,对应的将那个Image字段的内容赋给bytfile()
Open strfilename For Binary As #1
Put #1, , bytfile()
Close #1
x_x_j 2006-04-29
  • 打赏
  • 举报
回复
Sql Servr里有个Image类型,可以保存二进制信息,
Dim strfilename As String
Dim strfiletitle As String
Dim fillng As Long
Dim fillen As Long
Dim bytfile() As Byte

strfilename = CommonDialog1.FileName
strfiletitle = CommonDialog1.FileTitle
fillng = FreeFile
Open strfilename For Binary As #fillng
fillen = LOF(fillng)
If fillen > 8 Then
ReDim bytfile(fillen - 1)
Get #fillng, , bytfile()
End If
Close #fillng

将bytfile()赋给某个image字段就可以了
dongzhiqian 2006-04-29
  • 打赏
  • 举报
回复
使用ADO对象模型的Stream对象。
Stream对象可以对二进制(图像、声音)的数据库进行读写。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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