----- 数据库连接字符串模板 ---------------------------------------

'** 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服务器名"  

1、保存文件到数据库中

Sub s_SaveFile()  
    Dim iStm As ADODB.Stream  
    Dim iRe As ADODB.Recordset  
    Dim iConcStr As String  
    'ACCESS数据库的连接字符串  
    iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _  
        ";Data Source=F:My Documents客户资料1.mdb"  
    'SQL数据库的连接字符串  
    iConcStr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _  
        "User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名"  
    '读取文件到内容  
    Set iStm = New ADODB.Stream  
    With iStm  
        .Type = adTypeBinary    '二进制模式,如果是用text/ntext字段保存纯文本数据,则改用 adTypeText  
        .Open  
        .LoadFromFile "c: est.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  

2、从数据库中读取数据,保存成文件

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=\xzc$Inetpubzjzjzj.mdb"  
    '打开表  
    Set iRe = New ADODB.Recordset  
    iRe.Open "tb_img", iConc, adOpenKeyset, adLockReadOnly  
    iRe.Filter = "id=64"  

    if iRe("img").ActualSize>0 Then  
        '保存到文件  
        Set iStm = New ADODB.Stream  
        With iStm  
            .Mode = adModeReadWrite  
            .Type = adTypeBinary '二进制模式,如果是用text/ntext字段保存纯文本数据,则改用 adTypeText  
            .Open  
            .Write iRe("img")  
            .SaveToFile "c: est.doc"  
        End With  
        '关闭对象  
        iStm.Close  
    End If  
    iRe.Close  
End Sub