Click here to Skip to main content
6,295,667 members and growing! (14,716 online)
Email Password   helpLost your password?
Database » Database » General     Beginner

Using C# for Inserting CLOB data in Oracle

By ace

Inserting CLOB data into Oracle table using C# language
C# 2.0.NET 1.1, .NET 2.0, WinXPVS.NET2003, VS2005, DBA, Dev
Posted:4 Apr 2006
Updated:3 Aug 2007
Views:59,642
Bookmarked:17 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
11 votes for this article.
Popularity: 2.84 Rating: 2.73 out of 5
3 votes, 27.3%
1
2 votes, 18.2%
2
3 votes, 27.3%
3
2 votes, 18.2%
4
1 vote, 9.1%
5

Introduction

Inserting CLOB data in Oracle

Even the latest versions of Oracle's database 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 an SQL INSERT statement of inserting to column that has CLOB data type, but this way only strings no longer than 4,000 characters can be stored.

In order to store large amount of text, a procedure consisting of several steps is required. Below is the class sample 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(); 
        }   
    }
}

Then, please include that class into your project named clsOracle.cls
After all, creating button 'SAVE' to call this class with code showed 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 characteristic of CLOB addresing, we need to insert a record first 
to get it updated.

Then, task completed...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ace


Member
Agus Candra is the .NET Developer in Singapore. Now, he's trying to play around with C#, VB.net and Oracle database.

To contact Agus, email him at aguscandra@gmail.com
Occupation: Web Developer
Location: Indonesia Indonesia

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralACE please test your code before you post it. PinmemberJohn T. Emile10:33 28 May '08  
AnswerWhy so difficult? Pinmembertuinstoel9:07 7 Jan '08  
GeneralRe: Why so difficult? [modified] Pinmemberhungud0:41 30 Oct '08  
GeneralNull column Pinsusslimstephy20:41 15 Dec '07  
QuestionCan you not just use OracleType.Clob Pinmembershaneritchie14:33 9 Aug '07  
AnswerRe: Can you not just use OracleType.Clob Pinmemberace19:35 11 Aug '07  
AnswerRe: Can you not just use OracleType.Clob PinmemberDaniel A.0:52 16 Aug '07  
GeneralThanks a lot! ....Oracle.DataAccess version PinmemberNicola Costantini1:14 3 Aug '07  
GeneralRe: Thanks a lot! ....Oracle.DataAccess version Pinmemberace1:29 3 Aug '07  
GeneralInvalid Operation. The Connection is closed PinmemberVenkat Raman17:27 30 Jul '07  
GeneralRe: Invalid Operation. The Connection is closed Pinmemberace18:16 30 Jul '07  
GeneralRe: Invalid Operation. The Connection is closed Pinmemberbkeller8111:54 15 May '08  
QuestionWith stored procedure? Pinmemberyena_yena21:47 11 Jan '07  
AnswerRe: With stored procedure? Pinmemberace16:29 14 Jan '07  
GeneralRe: With stored procedure? Pinmembertuinstoel8:32 7 Jan '08  
GeneralWhere is INSERT? PinmemberAbishek Bellamkonda19:43 18 Apr '06  
GeneralRe: Where is INSERT? Pinmemberace17:18 19 Apr '06  
GeneralRe: Where is INSERT? PinmemberAbishek Bellamkonda18:11 19 Apr '06  
GeneralRe: Where is INSERT? Pinmemberace23:58 23 Apr '06  
GeneralSome improvements... Pinmemberhleuze22:31 11 Apr '06  
GeneralRe: Some improvements... Pinmemberace17:21 19 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Aug 2007
Editor:
Copyright 2006 by ace
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project