**************************************** *********************************
'**
'** Use ADODB. Stream to save/read files to the database
'** Reference Microsoft ActiveX Data Objects 2.5 Library and later
'**
'** ----- Database connection string template ---------------------------------------
'** ACCESS Database
'** IConcStr = "Provider = Microsoft. Jet. OLEDB.4.0; Persist Security Info = False "&_
'** "; Data Source = database name"
'**
'** SQL database
'** IConcStr = "Provider = SQLOLEDB.1; Persist Security Info = True ;"&_
'** "User ID = User name; 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 "&_
"; Data 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
'Open the table that saves the file
Set iRe = New ADODB. Recordset
With iRe
. Open "table", iConc, adOpenKeyset, adLockOptimistic
. Addnew' adds a record.
. Fields ("field for saving file content") = iStm. Read
. Update
End
'Close the object after completion
IRe. Close
IStm. Close
End Sub
'Read 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 "&_
"; Data Source =\\ xz \ c $ \ Inetpub \ zj. mdb"
'Open a 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
'Close the object
IRe. Close
IStm. Close
End Sub