Article ID: 192743
Article Last Modified on 3/2/2005
CREATE TABLE LARGTEXT (
MYID NUMBER(2) NOT NULL PRIMARY KEY,
BIGTEXT LONG
);
/
INSERT INTO LARGTEXT (MYID) VALUES (1);
/
Commit;Option Explicit
Dim Cn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim strConn As String
Dim strSQL As String
Dim FileLength As Long 'Used in Command1 and Command2 procedures.
Dim Numblocks As Integer
Dim LeftOver As Long
Dim i As Integer
Const BlockSize = 10000 'This size can be experimented with for performance and reliability.
Private Sub Form_Load()
Command1.Caption = "AppendChunk"
Command2.Caption = "GetChunk"
Command3.Caption = "Exit"
Command2.Enabled = False
'Make Connection.
'Set Cursor type.
strConn = "UID=MyUID;PWD=MyPassword;" & _
"driver={Microsoft ODBC for Oracle};" & _
"SERVER=MyServer;"
Set Cn = New ADODB.Connection
Cn.CursorLocation = adUseClient
Cn.Open strConn
Debug.Print Cn.ConnectionString
End Sub
Public Sub Command1_Click()
' AppendChunk button.
' This procedure prompts for a .txt file,
' converts that file to a String Variable,
' and saves the String Variable to the table
' using the Appendchunk method.
'
Dim TextFile As String
Dim strData As String 'String for LongVarChar data.
Dim SourceFile As Integer
' Open the LARGTEXT table.
strSQL = "Select MyID, BigText from LARGTEXT WHERE MyID = 1"
Set Rs = New ADODB.Recordset
Rs.CursorType = adOpenStatic
Rs.LockType = adLockOptimistic
Rs.Open strSQL, Cn
'Retrieve the text file and update the record.
CommonDialog1.Filter = "(*.txt)|*.txt"
CommonDialog1.ShowOpen
TextFile = CommonDialog1.filename
Me.MousePointer = vbHourglass
Me.Caption = "Retrieving the Text File"
' Save text file to the table column.
SourceFile = FreeFile
Open TextFile For Binary Access Read As SourceFile
FileLength = LOF(SourceFile) ' Get the length of the file.
Debug.Print "Filelength is " & FileLength
If FileLength = 0 Then
Close SourceFile
MsgBox TextFile & " empty or not found."
Exit Sub
Else
Numblocks = FileLength / BlockSize
LeftOver = FileLength Mod BlockSize
Rs(1).AppendChunk Null
strData = String(LeftOver, " ")
Get SourceFile, 1, strData
Rs(1).AppendChunk strData
strData = String(BlockSize, " ")
For i = 1 To Numblocks
Get SourceFile, , strData
Rs(1).AppendChunk strData
Next i
Close SourceFile
Rs.Update 'Commit the new data.
Close SourceFile
End If
Me.Caption = "Text File Retrieved"
Command2.Enabled = True
Me.MousePointer = vbNormal
End Sub
Private Sub Command2_Click()
' GetChunk Button.
' This procedure retrieves the text file
' from the table using the GetChunk method,
' converts the data to a file and
' displays that file in the RichTextBox.
'
Dim strData As String 'String for LongVarChar data
Dim DestFileNum As Integer
Dim DiskFile As String
Dim txtOutFile As String
Dim FileSize As Long
Me.MousePointer = vbHourglass
Me.Caption = "Getting the Text File"
' Remove any existing destination file.
DiskFile = App.Path & "\BigText.txt"
If Len(Dir$(DiskFile)) & 0 Then
Kill DiskFile
End If
DestFileNum = FreeFile
Open DiskFile For Binary As DestFileNum
Numblocks = FileLength / BlockSize
LeftOver = FileLength Mod BlockSize
FileSize = Rs(1).ActualSize
Debug.Print "The file size is " & FileSize
strData = String(FileSize, 32)
For i = 1 To Numblocks
strData = String(BlockSize, 32)
strData = Rs(1).GetChunk(BlockSize)
Put DestFileNum, , strData
Next i
strData = String(LeftOver, 32)
strData = Rs(1).GetChunk(LeftOver)
Put DestFileNum, , strData
Close DestFileNum
RichTextBox1.LoadFile DiskFile, rtfText
Rs.Close
Debug.Print "Complete"
Me.Caption = "Success!"
Me.MousePointer = vbNormal
End Sub
Private Sub Command3_Click()
'Exit button.
Cn.Close
Unload Me
End Sub
185958 HOWTO: Use ADO GetChunk/AppendChunk with Oracle for BLOB Data
175018 HOWTO: Acquire and Install the Microsoft Oracle ODBC Driver
Keywords: kbhowto kboracle kbprovider kbdatabase KB192743