?? form1.frm
字號(hào):
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對(duì)象
Dim iConcstr As String
Dim iConc As ADODB.Connection
Private Sub Form_Load()
'數(shù)據(jù)庫(kù)連接字符串
iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=C:\db1.mdb"
'連接sqlserver數(shù)據(jù)庫(kù)的語(yǔ)句寫(xiě)法是:
'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
'=========================================================
'保存文件到數(shù)據(jù)庫(kù)中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcstr As String
'讀取文件到內(nèi)容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二進(jìn)制模式
.Open
.LoadFromFile App.Path + "\test.jpg"
End With
'打開(kāi)保存文件的表
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
'完成后關(guān)閉對(duì)象
iRe.Close
iStm.Close
End Sub
Sub s_ReadFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
'打開(kāi)表
Set iRe = New ADODB.Recordset
'得到最新添加的紀(jì)錄
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")
'這里注意了,如果當(dāng)前目錄下存在test1.jpg,會(huì)報(bào)一個(gè)文件寫(xiě)入失敗的錯(cuò)誤.
.SaveToFile App.Path & "\test2.jpg"
End With
'關(guān)閉對(duì)象
iRe.Close
iStm.Close
End Sub
'=====================================================================
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -