在數(shù)據(jù)庫(kù)中存取文件
發(fā)表時(shí)間:2024-02-08 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在數(shù)據(jù)庫(kù)中存取文件'************************************************* '** '** 使用 ADODB.Stream 保存/讀取文件到數(shù)據(jù)庫(kù) '** 引用 Microsoft ActiveX Data Obje...
在數(shù)據(jù)庫(kù)中存取文件'*************************************************
'**
'** 使用 ADODB.Stream 保存/讀取文件到數(shù)據(jù)庫(kù)
'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
'**
'** ----- 數(shù)據(jù)庫(kù)連接字符串模板 ---------------------------------------
'** ACCESS數(shù)據(jù)庫(kù)
'** iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
'** ";Data Source=數(shù)據(jù)庫(kù)名"
'**
'** SQL數(shù)據(jù)庫(kù)
'** iConcStr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
'** "User ID=用戶名;Password=密碼;Initial Catalog=數(shù)據(jù)庫(kù)名;Data Source=SQL服務(wù)器名"
'**
'*************************************************
'
'保存文件到數(shù)據(jù)庫(kù)中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcStr As String
'數(shù)據(jù)庫(kù)連接字符串
iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=F:\My Documents\客戶資料1.mdb"
'讀取文件到內(nèi)容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二進(jìn)制模式
.Open
.LoadFromFile "c:\test.doc"
End With
'打開(kāi)保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "表", iConc, adOpenKeyset, adLockOptimistic
.AddNew '新增一條記錄
.Fields("保存文件內(nèi)容的字段") = iStm.Read
.Update
End With
'完成后關(guān)閉對(duì)象
iRe.Close
iStm.Close
End Sub
'從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù),保存成文件
Sub s_ReadFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConc As String
'數(shù)據(jù)庫(kù)連接字符串
iConc = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=\\xz\c$\Inetpub\zj\zj\zj.mdb"
'打開(kāi)表
Set iRe = New ADODB.Recordset
iRe.Open "tb_img", iConc, adOpenKeyset, adLockReadOnly
iRe.Filter = "id=64"
'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("img")
.SaveToFile "c:\test.doc"
End With
'關(guān)閉對(duì)象
iRe.Close
iStm.Close
End Sub