Click here to Skip to main content
Licence CPOL
First Posted 3 Apr 2006
Views 115,983
Bookmarked 30 times

Using C# for Inserting CLOB Data in Oracle

By | 3 Aug 2007 | Article
Inserting CLOB data into an Oracle table using C#.
 
Part of The SQL Zone sponsored by
See Also

Introduction

Even the latest versions of Oracle databases have a limitation on the size of strings that they can handle when storing and retrieving data from tables. Currently, this limitation is 4,000 characters. Prior to version 8.1, that was 2,000. While this limitation does not cause any problems for storing short strings and words, it becomes a serious obstacle when a programmer needs to store large volumes of text in one record.

Oracle provides a special column data type called Character Large Object (CLOB) that allows storage up to 4 megabytes of character data. It is, however, very difficult to store such a huge amount of data in the table. What Oracle actually stores in the table is just a pointer to the place in the data store where the actual data is stored. This technique is the root of a more complicated procedure that's needed first to store data in the CLOB column, and then to store other 'primitive' data types. It is still possible to pass a string to a SQL INSERT statement of insert to a column that has CLOB data type, but only strings no longer than 4,000 characters can be stored in this way.

In order to store large amounts of text, a procedure consisting of several steps is required. Below is a class sample of how this can be done:

using System;

namespace InsertingCLOB
{
    public class clsOracle
    {     
        private System.Data.OracleClient.OracleConnection connOracle;
        private System.Data.OracleClient.OracleDataReader rstOracle;
        private System.Data.OracleClient.OracleCommand sqlCommandOracle;
        private System.Data.OracleClient.OracleTransaction txn; 
        private System.Data.OracleClient.OracleLob clob;    
        
        public clsOracle()    
        {
            string p_conn_db= "Data Source=" + OracleIP + ";User ID=" + 
                              OracleUserName + ";PASSWORD=" + OraclePassword + ";";
            connOracle = new System.Data.OracleClient.OracleConnection(p_conn_db);
            connOracle.Open();
        }   

        public void InsertRecord(string SQLStatement)
        { 
            if (SQLStatement.Length>0)
            {
                if(connOracle.State.ToString().Equals("Open"))
                { 
                    sqlCommandOracle = 
                      new System.Data.OracleClient.OracleCommand(SQLStatement,connOracle);
                    sqlCommandOracle.ExecuteScalar(); 
                }
            }
        }

        public void InsertCLOB(string SQLStatement, string str)    
        {
            if (SQLStatement.Length>0)
            {
                if(connOracle.State.ToString().Equals("Open"))
                {                        
                    byte[] newvalue = System.Text.Encoding.Unicode.GetBytes(str);
                    sqlCommandOracle= 
                      new System.Data.OracleClient.OracleCommand(SQLStatement,connOracle);
                    rstOracle = sqlCommandOracle.ExecuteReader();
                    rstOracle.Read();
                    txn = connOracle.BeginTransaction();
                    clob = rstOracle.GetOracleLob(0);
                    clob.Write(newvalue, 0, newvalue.Length);
                    txn.Commit();
                }
            }
        }
        public void CloseDatabase()
        {
            connOracle.Close();    
            connOracle.Dispose(); 
        }   
    }
}

Include this class named clsOracle.cls in your project. Now, create a button 'Save' to call this class with the code shown below:

private void btnSave_Click(object sender, System.EventArgs e)
{
    clsOracle db=new clsOracle();   

    // example for primary key    
    string field_id = "1";         
    // insert 2 characters for addresing    
    string field_temp = "XX";
    
    string sql = "Insert into table_nm values('" + field_id + 
                 "', '" + field_temp + "')"; 
    db.InsertRecord(sql); 

    sql="select news_text from table_nm +
    "WHERE field_nm'" + field_id + "' FOR UPDATE";
    
    db.InsertCLOB(sql, txtCLOBData.Text.ToString()); 
    db.CloseDatabase();
}

Regarding the characteristics of CLOB addressing, we need to insert a record first to get it updated. Then, the task is completed...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ace

Web Developer

Singapore Singapore

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFlexible Datatype PinmemberAjay Kale New0:26 11 Nov '10  
GeneralMy vote of 1 PinmemberAnton Kratenok1:12 16 Oct '09  
GeneralACE please test your code before you post it. PinmemberJohn T. Emile9:33 28 May '08  
QuestionWhy so difficult? Pinmembertuinstoel8:07 7 Jan '08  
AnswerRe: Why so difficult? [modified] Pinmemberhungud23:41 29 Oct '08  
GeneralNull column Pinsusslimstephy19:41 15 Dec '07  
QuestionCan you not just use OracleType.Clob Pinmembershaneritchie13:33 9 Aug '07  
AnswerRe: Can you not just use OracleType.Clob Pinmemberace18:35 11 Aug '07  
AnswerRe: Can you not just use OracleType.Clob PinmemberDaniel A.23:52 15 Aug '07  
GeneralThanks a lot! ....Oracle.DataAccess version PinmemberNicola Costantini0:14 3 Aug '07  
GeneralRe: Thanks a lot! ....Oracle.DataAccess version Pinmemberace0:29 3 Aug '07  
GeneralInvalid Operation. The Connection is closed PinmemberVenkat Raman16:27 30 Jul '07  
GeneralRe: Invalid Operation. The Connection is closed Pinmemberace17:16 30 Jul '07  
GeneralRe: Invalid Operation. The Connection is closed Pinmemberbkeller8110:54 15 May '08  
AnswerRe: Invalid Operation. The Connection is closed Pinmemberasousaaraujo13:19 24 Feb '10  
QuestionWith stored procedure? Pinmemberyena_yena20:47 11 Jan '07  
Your code work correctly with text sql statement.
But I want use stored procedure (like this below), but don't work. Frown | :(
 
PROCEDURE Test(p_field IN pls_integer)
AS
 
v_temp clob;
 
BEGIN

select field2 into v_temp from table
WHERE field = p_field
FOR UPDATE;

END Test;
 
Thanks.
 
Sara
AnswerRe: With stored procedure? Pinmemberace15:29 14 Jan '07  
GeneralRe: With stored procedure? Pinmembertuinstoel7:32 7 Jan '08  
QuestionWhere is INSERT? PinmemberAbishek Bellamkonda18:43 18 Apr '06  
AnswerRe: Where is INSERT? Pinmemberace16:18 19 Apr '06  
GeneralRe: Where is INSERT? PinmemberAbishek Bellamkonda17:11 19 Apr '06  
GeneralRe: Where is INSERT? Pinmemberace22:58 23 Apr '06  
GeneralSome improvements... Pinmemberhleuze21:31 11 Apr '06  
GeneralRe: Some improvements... Pinmemberace16:21 19 Apr '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 3 Aug 2007
Article Copyright 2006 by ace
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid