Access to files in the database __ database

Source: Internet
Author: User
Tags save file access database

'*************************************************************************
'**
' * * 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.