1,216
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Dim mdbConn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click() '退出
rs.Close
mdbConn.Close
Set rs = Nothing
Set mdbConn = Nothing
Unload Me
End Sub
Private Sub ViewData()
Dim B() As Byte
With rs
Text1.Text = .Fields(0)
B = .Fields(1).Value
Dim Bag As New PropertyBag
Bag.Contents = B
Set Picture1.Picture = Bag.ReadProperty("Image")
End With
End Sub
Private Sub Command2_Click() '新增
Dim Bag As New PropertyBag
Dim B() As Byte
Bag.WriteProperty "Image", Picture1.Image
ReDim B(LenB(Bag.Contents))
B = Bag.Contents
rs.AddNew
rs.Fields(0) = "'" & Replace(Text2, ".", "")
rs.Fields(1).Value = B
rs.Update
Set Bag = Nothing
End Sub
Private Sub Command3_Click() '浏览
CommonDialog1.Filter = "BMP文件(*.bmp)|*.bmp|JPEG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif"
CommonDialog1.ShowOpen
Picture1.Picture = LoadPicture(CommonDialog1.FileName)
Text2.Text = CommonDialog1.FileTitle
End Sub
Private Sub CmdMoveData_Click(Index As Integer)
With rs
Select Case Index
Case 0 '移到第一条记录
If Not .BOF Then .MoveFirst
Case 1 '移到上一条记录
If .RecordCount > 0 Then
If .BOF = False Then .MovePrevious
If .BOF = True Then
.MoveFirst
MsgBox "记录已经移到第一条!", vbOKOnly, Me.Caption
End If
End If
Case 2 '移到下一条记录
If .RecordCount > 0 Then
If .EOF = False Then .MoveNext
If .EOF = True Then
.MoveLast
MsgBox "记录已经移到最后一条!", vbOKOnly, Me.Caption
End If
End If
Case 3 '移到最后一条记录
If .RecordCount > 0 Then
If Not .EOF = True Then .MoveLast
End If
End Select
ViewData
Text3.Text = "第 " & rs.AbsolutePosition & " 条记录"
End With
End Sub
Private Sub Command4_Click()
Dim Bag As New PropertyBag
Dim B() As Byte
Bag.WriteProperty "Image", Picture1.Image
ReDim B(LenB(Bag.Contents))
B = Bag.Contents
rs.Fields(1).Value = B
rs.Update
Set Bag = Nothing
End Sub
Private Sub Command5_Click()
rs.Delete adAffectCurrent
rs.Update
CmdMoveData_Click (1)
End Sub
Private Sub Form_Load()
Dim lsDbFile As String
lsDbFile = App.Path & "\db.mdb"
mdbConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & lsDbFile & ";Persist Security Info=False"
rs.Open "select * from tabtemp", mdbConn, adOpenKeyset, adLockOptimistic
If rs.RecordCount <> 0 Then
ViewData
rs.MoveFirst
Text3.Text = "第 " & rs.AbsolutePosition & " 条记录"
End If
End Sub
Private Sub Command1_Click()
'新增一张图片
Dim Bag As PropertyBag
Dim Buff() As Byte
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set Bag = New PropertyBag
Bag.WriteProperty "Image", Picture1.Image
ReDim Buff(LenB(Bag.Contents))
Buff = Bag.Contents
'Set cn = ConnectionToDB
Set rs = New ADODB.Recordset
rs.Open "select img from tb_image where 1=0", _
cn, adOpenKeyset, adLockOptimistic
rs.AddNew
rs.Fields("img") = Buff
rs.Update
Set rs = Nothing
Set cn = Nothing
Set Bag = Nothing
MsgBox "OK"
End Sub
Private Sub Command2_Click()
'读出全部图片
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Bag As PropertyBag
Dim Buff() As Byte
Dim t
'Set cn = ConnectionToDB
Set rs = New ADODB.Recordset
rs.Open "Select * From tb_image", _
cn, adOpenKeyset, adLockOptimistic
While Not rs.EOF
Buff = rs.Fields("Img").Value
Set Bag = New PropertyBag
Bag.Contents = Buff
Call Bag.WriteProperty("Image", Buff)
Set Picture1.Picture = Bag.ReadProperty("Image")
'延时
t = Timer
Do
DoEvents
Loop While Timer - t < 1
rs.MoveNext
Set Bag = Nothing
Wend
set rs=nothing
set cn=nothing
End Sub