[求助]通过vb如何在sql中存、取图片
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim stm As ADODB.Stream
Dim strFileName As String
Private Sub cmbLoad_Click()
Dim rs As ADODB.Recordset
Dim stm As ADODB.Stream
Dim strFileName As String
Private Sub cmbLoad_Click()
Me.CommonDialog1.Filter = "jpg|*.jpg|gif|*.gif|bmp|*.bmp"
Me.CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
strFileName = CommonDialog1.FileName
Me.Picture1.Picture = LoadPicture(strFileName)
End If
End Sub
'保存图片到数据库
Private Sub cmdSave_Click()
If strFileName <> "" And txtName.Text = "" Then
MsgBox "头像和昵称都不能为空"
Exit Sub
End If
stm.Type = adTypeBinary '类型为一二进制数组
stm.Open
stm.LoadFromFile Me.CommonDialog1.FileName '图片路径
rs.Open "select * from Test", cn, adOpenKeyset, adLockOptimistic
rs.AddNew
rs.Fields("Name").Value = txtName.Text
rs.Fields("Pho").Value = stm.Read
rs.Update
rs.Close
stm.Close
End Sub
'从数据库读取图片到Picture1
Private Sub cmdshow_Click()
If txtName.Text = "" Then
MsgBox "请输入昵称"
Exit Sub
End If
rs.Open "select * from Test where Name='" & txtName.Text & "'", cn, adOpenKeyset, adLockOptimistic
Set Me.Picture1.DataSource = rs
Me.Picture1.DataField = "pho"
rs.Close
End Sub
Me.CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
strFileName = CommonDialog1.FileName
Me.Picture1.Picture = LoadPicture(strFileName)
End If
End Sub
'保存图片到数据库
Private Sub cmdSave_Click()
If strFileName <> "" And txtName.Text = "" Then
MsgBox "头像和昵称都不能为空"
Exit Sub
End If
stm.Type = adTypeBinary '类型为一二进制数组
stm.Open
stm.LoadFromFile Me.CommonDialog1.FileName '图片路径
rs.Open "select * from Test", cn, adOpenKeyset, adLockOptimistic
rs.AddNew
rs.Fields("Name").Value = txtName.Text
rs.Fields("Pho").Value = stm.Read
rs.Update
rs.Close
stm.Close
End Sub
'从数据库读取图片到Picture1
Private Sub cmdshow_Click()
If txtName.Text = "" Then
MsgBox "请输入昵称"
Exit Sub
End If
rs.Open "select * from Test where Name='" & txtName.Text & "'", cn, adOpenKeyset, adLockOptimistic
Set Me.Picture1.DataSource = rs
Me.Picture1.DataField = "pho"
rs.Close
End Sub
Private Sub Form_Load()
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set stm = New ADODB.Stream
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Test.mdb;" '把这句换成连接SQL的字符串
cn.Open
End Sub
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set stm = New ADODB.Stream
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=Test.mdb;" '把这句换成连接SQL的字符串
cn.Open
End Sub