'*************************************************************************
'**
' * * Use ADODB. Stream save/Read files to database
' * * Reference the Microsoft ActiveX Data Objects 2.5 Library and the above version
'**
' * *-----database connection string Template---------------------------------------
' * * Access database
' * * iconcstr = provider=microsoft.jet.oledb.4.0; Persist Security Info=false "& _
' * * ";D ATA source= database name"
'**
' * * SQL database
' * * iconcstr = Provider=SQLOLEDB.1; Persist security Info=true; "& _
' * * ' user id= username; password= password; Initial catalog= database name; Data source=sql server name "
'**
'*************************************************************************
'
' Save the file to the database
Sub S_savefile ()
Dim IStm as ADODB. Stream
Dim IRe as ADODB. Recordset
Dim Iconcstr as String
' Database connection string
Iconcstr = "Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=false "& _
";D ATA source=f:/my documents/customer Profile 1.mdb"
' Read the file to the content
Set IStm = New ADODB. Stream
With IStm
. Type = adTypeBinary ' binary mode
. Open
. LoadFromFile "C:/test.doc"
End With
' Open the saved file's table
Set iRe = New ADODB. Recordset
With IRe
. Open "Table", Iconc, adOpenKeyset, adLockOptimistic
. AddNew ' Add a record
. Fields ("Fields to save file contents") = Istm.read
. Update
End With
' Close object when finished
Ire.close
Istm.close
End Sub
' Read the data from the database and save it as a file
Sub S_readfile ()
Dim IStm as ADODB. Stream
Dim IRe as ADODB. Recordset
Dim Iconc as String
' Database connection string
Iconc = "Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=false "& _
";D ata Source=//xz/c$/inetpub/zj/zj/zj.mdb"
' Open Table
Set iRe = New ADODB. Recordset
Ire.open "Tb_img", Iconc, adOpenKeyset, adLockReadOnly
Ire.filter = "Id=64"
' Save to File
Set IStm = New ADODB. Stream
With IStm
. Mode = adModeReadWrite
. Type = adTypeBinary
. Open
. Write iRe ("img")
. SaveToFile "C:/test.doc"
End With
' Close Object
Ire.close
Istm.close
End Sub