' Alastair Vance ([email protected]) ' 26-FEB-2002 ' ' Project to test blob import and export ' from an oracle (8.1.7.2) database using ADO and ' Software Artisans FileManager component. ' ' Oracle 8.1.7.2 client used. ' ' VB Project References needed: ' Microsoft ActiveX Data Objects Library 2.x ' FileManager 1.1 ' ' Oracle Database Table Script: ' 数据库中建张表,写三个字段 ' CREATE TABLE BLOBTEST ( ' DOCTITLE VARCHAR2 (50), ' DOCDESC VARCHAR2 (255), ' DOCBIN BLOB ) ;'// 按路径 c:\book1.xls 随便建立一个.xls文件 '//下载的程序中有SAFileMgr.dll,注册后,添加引用,方可使用。 Option Explicit'//按钮,把文件加到数据库中 Private Sub CmdIn_Click() Dim Conn As New ADODB.Connection Dim Rs As New ADODB.Recordset Dim FileMgr As New FileManager
Dim SQL As String
' Using OLEDB to connect Conn.Provider = "OraOLEDB.Oracle" Conn.Open "servicename", "userid", "userpwd"
' Delete all current rows SQL = "Truncate table blobtest" Conn.Execute SQL
' Add new row with file SQL = "Select * from blobtest where 1=2" Rs.Open SQL, Conn, 2, 3
Rs.AddNew Rs("doctitle").Value = "Test " & Now Rs("docdesc").Value = "Description of Document." ' Puts file into database blob FileMgr.ExportToBlob "c:\book1.xls", Rs("docbin")
Rs.Update
' Close recordset and database connection Rs.Close Conn.Close
Set Conn = Nothing
MsgBox "Finished."End Sub'//按钮,把数据库中的文件输出到磁盘上Private Sub CmdOut_Click() Dim Conn As New ADODB.Connection Dim Rs As New ADODB.Recordset Dim FileMgr As New FileManager
Dim SQL As String Dim DocTitle As String Dim DocDesc As String
' Read all from table SQL = "Select * from blobtest" Rs.Open SQL, Conn, 0, 1
' Put data fields into local variables for ' use later on (after recordset is closed) DocTitle = Rs("doctitle") DocDesc = Rs("docdesc") ' Get and save file at specified location. ' I use a timestamp here to assure uniqueness of ' the filename. You could simply check that the ' file exists first before choosing a filename. FileMgr.ImportFromBlob Rs("docbin"), "c:\" & GetTimeStamp & "newmanual.xls"
' Close recordset and database connection Rs.Close Conn.Close
Set Conn = Nothing
' Inform user that process has finished. MsgBox "Blob extracted.", , DocTitle
End Sub Private Function GetTimeStamp() As String ' Using the time and date I can create a unique ' number for the application to use. GetTimeStamp = Replace(Replace(Replace(Now, ":", ""), "", ""), "/", "") End Function
学习ing.
有vb源代码,看看就明白了,可以实现往数据库中添加文件等大对象。
' 26-FEB-2002
'
' Project to test blob import and export
' from an oracle (8.1.7.2) database using ADO and
' Software Artisans FileManager component.
'
' Oracle 8.1.7.2 client used.
'
' VB Project References needed:
' Microsoft ActiveX Data Objects Library 2.x
' FileManager 1.1
'
' Oracle Database Table Script:
' 数据库中建张表,写三个字段
' CREATE TABLE BLOBTEST (
' DOCTITLE VARCHAR2 (50),
' DOCDESC VARCHAR2 (255),
' DOCBIN BLOB ) ;'// 按路径 c:\book1.xls 随便建立一个.xls文件
'//下载的程序中有SAFileMgr.dll,注册后,添加引用,方可使用。
Option Explicit'//按钮,把文件加到数据库中
Private Sub CmdIn_Click() Dim Conn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Dim FileMgr As New FileManager
Dim SQL As String
' Using OLEDB to connect
Conn.Provider = "OraOLEDB.Oracle"
Conn.Open "servicename", "userid", "userpwd"
' Delete all current rows
SQL = "Truncate table blobtest"
Conn.Execute SQL
' Add new row with file
SQL = "Select * from blobtest where 1=2"
Rs.Open SQL, Conn, 2, 3
Rs.AddNew
Rs("doctitle").Value = "Test " & Now
Rs("docdesc").Value = "Description of Document."
' Puts file into database blob
FileMgr.ExportToBlob "c:\book1.xls", Rs("docbin")
Rs.Update
' Close recordset and database connection
Rs.Close
Conn.Close
Set Conn = Nothing
MsgBox "Finished."End Sub'//按钮,把数据库中的文件输出到磁盘上Private Sub CmdOut_Click() Dim Conn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Dim FileMgr As New FileManager
Dim SQL As String
Dim DocTitle As String
Dim DocDesc As String
' OLEDB connection
Conn.Provider = "OraOLEDB.Oracle"
Conn.Open "servicename", "userid", "userpwd"
' Read all from table
SQL = "Select * from blobtest"
Rs.Open SQL, Conn, 0, 1
' Put data fields into local variables for
' use later on (after recordset is closed)
DocTitle = Rs("doctitle")
DocDesc = Rs("docdesc")
' Get and save file at specified location.
' I use a timestamp here to assure uniqueness of
' the filename. You could simply check that the
' file exists first before choosing a filename.
FileMgr.ImportFromBlob Rs("docbin"), "c:\" & GetTimeStamp & "newmanual.xls"
' Close recordset and database connection
Rs.Close
Conn.Close
Set Conn = Nothing
' Inform user that process has finished.
MsgBox "Blob extracted.", , DocTitle
End Sub
Private Function GetTimeStamp() As String ' Using the time and date I can create a unique
' number for the application to use.
GetTimeStamp = Replace(Replace(Replace(Now, ":", ""), "", ""), "/", "")
End Function