Solved

Question about VB and CLOB field??

Posted on 2001-06-14
13
915 Views
Last Modified: 2012-06-21
Hi all,
My database is Oracle 8.0.5 and my program write by Visual Basic. In order to store large data (> 32k), I have use CLOB datatype fields. But I don't know how to INSERT or UPDATE data to CLOB field by call from VB? Please help me. And I wonder if you mind to send me some source code?
Please help me as soon as possible.
Thank you very much.
Thinh
0
Comment
Question by:xuanthinh
13 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6190565
You need to use the AppendChunk method in ADO in order to handle these sort of fields. SQL Server Text and Binary fields and Oracle BLOB/CLOB fields require a different method of insert/update than "normal" fields. The first thing to do is to check out the ADO documentation on the AppendChunk method, there is a good example of its use (though focused to SQL Server) but you should be able to tailor this to the specifics of your situation. Unfortunately I don't have any Oracle specific code as that is not one of my areas of experience.
0
 
LVL 15

Expert Comment

by:lyonst
ID: 6190617
Hi,

HOWTO: Sample Functions Demonstrating GetChunk and AppendChunk

http://support.microsoft.com/support/kb/articles/Q194/9/75.ASP

Sample Code included in above link.

Cheers,

T.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6190752
Heres an example of the CLOB/BLOB for inserting ...
Private Sub InsertEntryField(ByVal id_entry As Long, ByVal od_field As Variant, ByVal has_children As
Variant, ByVal od_father As Variant, ByVal tag_field As Variant, Optional ByVal txt_field As String,
Optional ByRef imgage_array As Variant)
On Error GoTo ErrorHandler
Dim Conn As New ADODB.Connection
Dim Rs As ADODB.Recordset
Dim sqlclause As String

   sqlclause = "SELECT identry, odfield, haschildren, odfather, tagfield, txtfield, imgfield FROM entryfield
WHERE 1 <> 1"
   Conn.Open Env.GetConnectionString
   Set Rs = New ADODB.Recordset
   Rs.Open sqlclause, Conn, adOpenKeyset, adLockOptimistic
   Rs.AddNew
   Rs.fields("identry").Value = SQLEncode(id_entry)
   Rs.fields("odfield").Value = SQLEncode(od_field)
   Rs.fields("haschildren").Value = SQLEncode(has_children)
   Rs.fields("odfather").Value = SQLEncode(od_father)
   Rs.fields("tagfield").Value = tag_field
   Rs.Update
   If Not IsMissing(txt_field) Then
       Rs.fields("txtfield").AppendChunk txt_field
       Rs.Update
   End If
   If Not IsMissing(imgage_array) Then
       Rs.fields("imgfield").AppendChunk imgage_array
       Rs.Update
   End If
   Rs.Close
   Set Rs = Nothing
   Conn.Close
   Set Conn = Nothing
   Exit Sub
ErrorHandler:
   Abort "InsertEntryField"
   Err.Raise Err.Number, Err.Source, RTErrDescription(TypeName(Me), "InsertEntryField", Err.Description)
End Sub

The image_array variable is a variant array created from a binary file existing on the hard disk...

   fhandle = FreeFile
   Open fname For Binary Access Read As fhandle
   binary_array = InputB(LOF(fhandle), fhandle)
   Close fhandle
The text_field variable is just a string
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6190767
Here's one for retrieving data from a BLOB....

Public Function GetDocument(ByVal id As Long, ByVal od As Integer) As String
On Error GoTo ErrorHandler
   Dim Conn As New ADODB.Connection
   Dim Rs As ADODB.Recordset
   Dim sqlclause As String
   Dim filehandle As Long
   Dim docobject() As Byte, docsize As Variant, docoffset As Long
   CheckState "GetDocument"
   ' get an image from the entryfield table and store it to a temporary file
   Set Conn = OpenNewConnection("publication")
   sqlclause = "SELECT identry, imgfield FROM entryfield" & _
               " WHERE identry = " & SQLEncode(id) & " AND odfield = " & SQLEncode(od)
   Set Rs = Conn.Execute(sqlclause)
   docsize = SQLDecode(Rs.Fields("imgfield").ActualSize)
   If docsize <> "" And docsize <> 0 Then
       temp_filename = doc_directory & "tempentryfield" & id & ".bin"
       If Dir(temp_filename) = "" Then
           ReDim docobject(docsize)
           docobject() = Rs.Fields("imgfield").GetChunk(docsize)
           filehandle = FreeFile
           Open temp_filename For Binary Access Write As filehandle
           Put filehandle, , docobject()
           Close filehandle
       End If
       IsGot = True
   Else
       temp_filename = ""
       IsGot = False
   End If
   Conn.Close
   Set Conn = Nothing
   GetDocument = temp_filename
   Exit Function
ErrorHandler:
   Err.Raise Err.Number, Err.Source, "GetDocument: " & Err.Description
End Function

And here's one for retrieving a CLOB...

Public Sub Create(ByVal an_env As String, ByVal id_entry As Long, ByVal od_field As Integer)
On Error GoTo error_handler
Dim Conn As New ADODB.Connection
Dim entryfield_Rs As ADODB.Recordset
Dim sqlclause As String
Dim fieldsize As Long
   
   LoadEnv an_env
   'object_status = OS_INITIALIZED
   sqlclause = "SELECT identry,odfield,haschildren,odfather,tagfield,txtfield FROM " & _
        "entryfield WHERE identry = " & _
       SQLEncode(id_entry) & " AND odfield = " & SQLEncode(od_field)
   Conn.Open Env.GetConnectionString
   Set entryfield_Rs = Conn.Execute(sqlclause)
   If entryfield_Rs.EOF Then
       Conn.Close
       Set Conn = Nothing
       Err.Raise PtolemyFatal, Err.Source, "Cannot find record where identry = " & id_entry & " and
odfield = " & od_field
   End If
   
   With entryfield_Rs
       identry = SQLDecode(.Fields("identry").Value)
       odfield = SQLDecode(.Fields("odfield").Value)
       haschildren = SQLDecode(.Fields("haschildren").Value)
       odfather = SQLDecode(.Fields("odfather").Value)
       tagfield = SQLDecode(.Fields("tagfield").Value)
       fieldsize = .Fields("txtfield").ActualSize
       If fieldsize > 0 Then
           txtfield = .Fields("txtfield").GetChunk(fieldsize)
       Else
           txtfield = ""
       End If
   End With
   is_new = False
   Conn.Close
   Set Conn = Nothing
   Exit Sub
error_handler:
   Abort "Create"
   Err.Raise Err.Number, Err.Source, RTErrDescription(TypeName(Me), "Create", Err.Description & " "
& sqlclause)
End Sub

0
 

Author Comment

by:xuanthinh
ID: 6193846
Thank you very much.
I have done as you said but Visual Basic put a error message "Data type is not supported" when it get SELECT sql. I think because the CLOB field but I don't know why?

This is my Example:
Database Oracle 8.0.5: I have this table CLOBS

CREATE TABLE clobs
 (
  id                         VARCHAR2(255),
  clob_col                   CLOB
 )
 PCTFREE    10
 PCTUSED    40
 INITRANS   1
 MAXTRANS   255
/

Application Visual Basic 6:
Connection by ADO 6 SP4
My SQL: "SELECT ID, CLOB_COL FROM CLOBS"

What is my error? I would like to get your answer.
Thank,
Thinh
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6194189
Could you send all the code concerning the connection, recordset etc please?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:xuanthinh
ID: 6197677
Thank you very much.

This is my Example:
Database Oracle 8.0.5: I have this table CLOBS

CREATE TABLE clobs
(
 id                         VARCHAR2(255),
 clob_col                   CLOB
)
PCTFREE    10
PCTUSED    40
INITRANS   1
MAXTRANS   255
/

Application Visual Basic 6 SP4:
Connection by ADO 2.5 :
This is my program:
In VB6 you open a new project, Form1 as default.
Add following objects in form1
----------------------------------------
Object           Name         Caption
----------------------------------------
RichTextBox      txtCLOB
CommandButton    cmdInsert    Insert
CommandButton    cmdUpdate    Update
CommandButton    cmdDelete    Delete
----------------------------------------

And paste following code in Form1
Option Explicit
Dim Db As New ADODB.Connection
Dim WithEvents rs As ADODB.Recordset

Dim i As Byte

Private Sub cmdDelete_Click()
    rs.Delete
End Sub

Private Sub cmdInsert_Click()
    i = i + 1
    rs.AddNew
    rs.Fields("id").Value = Str(i)
    rs.Fields("clob_col").Value = Me.txtCLOB.Text
    rs.Update
End Sub

Private Sub cmdUpdate_Click()
    rs.Update
End Sub

Private Sub Form_Load()
    i = 0
    Set Db = New ADODB.Connection
    Db.CursorLocation = adUseClient
    Db.Open "Provider=MSDAORA.1;Password=mypassword;User ID=myuser;Data Source=mydatabase;Persist Security Info=True"
   
    Set rs = New ADODB.Recordset
    rs.Open "Select id, clob_col from clobs", Db, adOpenKeyset, adLockOptimistic
   
End Sub

And Run Project I get a error message "Data type is not supported" when VB get SELECT sql. I think because the CLOB field but I don't know why?

And other question: If I replace field datatype CLOB to LONG, that error message will disappear. To update the LONG field I use function AppendChunk and GetChunk but result after GetChunk not same as before AppendChunk

What is my error? I would like to get your answer.
Thank,
Thinh
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6201767
You must do a select before the AddNew, the recordset is not yet initialised with the field types. Something like...

sqlclause = "SELECT identry, odfield, haschildren, odfather, tagfield, txtfield, imgfield FROM entryfield

WHERE 1 <> 1"
  Conn.Open Env.GetConnectionString
  Set Rs = New ADODB.Recordset
  Rs.Open sqlclause, Conn, adOpenKeyset, adLockOptimistic
  Rs.AddNew


' You will now have an empty recordset with the fields initialised correctly
0
 

Author Comment

by:xuanthinh
ID: 6206080
Thank you
If my field is LONG type then program does not have error. But if my field is CLOB type then program have error message "Data type is not supported" when VB get SELECT sql. What is version of Oracle support LOB datatype.
Thank,
Thinh
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6206315
I, personally have 8.1i, but I assume that CLOB, LOB must be supported by previous versions. What versiob of ADODB?

Maybe the solution lies here...

"Provider=MSDAORA.1;Password=mypassword;User ID=myuser;Data Source=mydatabase;Persist Security
Info=True"

I use "Provider=OraOLEDB.Oracle;Data Source=gdb.santdb;User ID=xxxx;PASSWORD=xxxx;"

What is your provider type?
0
 

Author Comment

by:xuanthinh
ID: 6224322
Thank,
I try to do as you but Oracle has error message "Provider cannot be found. It may not be properly installed". Would you like to point me the driver for OraOLEDB?
Thank you very much.
Thinh,
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7136139
Hi xuanthinh,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Refund points and save as a 0-pt PAQ.
*** PAQ-able info here, but ultimately, xuanthinh's problemn was not solved.  Suggest post to ORACLE TA.

EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 7173337
Question placed in PAQ

Computer101
E-E Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now