|
************************************************************************* '** '** 使用 ADODB.Stream 保存/读取文件到数据库 '** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本 '** '** ----- 数据库连接字符串模板 --------------------------------------- '** ACCESS数据库 '** iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _ '** ";Data Source=数据库名" '** '** SQL数据库 '** iConcStr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _ '** "User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名" '** '************************************************************************* ' '保存文件到数据库中 Sub s_SaveFile() Dim iStm As ADODB.Stream Dim iRe As ADODB.Recordset Dim iConcStr As String
'数据库连接字符串 iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _ ";Data Source=F:\My Documents\客户资料1.mdb"
'读取文件到内容 Set iStm = New ADODB.Stream With iStm .Type = adTypeBinary '二进制模式 .Open .LoadFromFile "c:\test.doc" End With
'打开保存文件的表 Set iRe = New ADODB.Recordset With iRe .Open "表", iConc, adOpenKeyset, adLockOptimistic .AddNew '新增一条记录 .Fields("保存文件内容的字段") = iStm.Read .Update End With
'完成后关闭对象 iRe.Close iStm.Close End Sub
'从数据库中读取数据,保存成文件 Sub s_ReadFile() Dim iStm As ADODB.Stream Dim iRe As ADODB.Recordset Dim iConc As String
'数据库连接字符串 iConc = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _ ";Data Source=\\xz\c$\Inetpub\zj\zj\zj.mdb"
'打开表 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
'关闭对象 iRe.Close iStm.Close End Sub |
|