📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 4380
ClientLeft = 60
ClientTop = 375
ClientWidth = 5130
LinkTopic = "Form1"
ScaleHeight = 4380
ScaleWidth = 5130
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command2
Caption = "save"
Height = 495
Left = 3240
TabIndex = 1
Top = 3600
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "read"
Height = 495
Left = 840
TabIndex = 0
Top = 3600
Width = 1215
End
Begin VB.Image Image1
Height = 3255
Left = 360
Stretch = -1 'True
Top = 120
Width = 4455
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
'2.5版本以下不支持Stream对象
Dim iConcstr As String
Dim iConc As ADODB.Connection
Private Sub Form_Load()
'数据库连接字符串
iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=C:\db1.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
Private Sub Command1_Click()
Call s_ReadFile
End Sub
Private Sub Command2_Click()
Call s_SaveFile
End Sub
'=========================================================
'保存文件到数据库中
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 membership", iConc, 1, 3
.AddNew '新增一条记录
.Fields("id") = "4"
.Fields("name") = "ahla"
.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 * from membership where id='2'", iConc, adOpenKeyset, adLockReadOnly
'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("photo")
'这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
.SaveToFile App.Path & "\test2.jpg"
End With
'关闭对象
iRe.Close
iStm.Close
End Sub
'=====================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -