Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear all,
I've a problem that i can't solved. I wanna insert data (XML) into a clob field and when i tried to write data into clob, its return an error.
This is my code :

C#
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 OracleServerAkses = "Data Source=(DESCRIPTION="
           + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.111.3.54)(PORT=1521)))"
           + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=kseistpd)));"
           + "User Id=AKSES;Password=password;";
      connOracle = new System.Data.OracleClient.OracleConnection(OracleServerAkses);
      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();
    }
  }



And this is the main class :

C#
static void Main(string[] args)
    {
      clsOracle db = new clsOracle();
      string guid = "guid12";
      string sql = "INSERT INTO T_CLOB(GUID, MEMBERID, INPUT_DATE) VALUES('" +
        guid + "' , 'BCA02', sysdate)";
      db.InsertRecord(sql);
      sql = "SELECT MESSAGES FROM T_CLOB WHERE GUID='" + guid + "' FOR UPDATE";
      db.InsertCLOB(sql, data);
      db.CloseDatabase();}

Fields of table in my database (Oracle) :
1. GUID (varchar2)
2. MESSAGES (clob)
3. INPUT_DATE (varhcar2)


Please help me.. :~:~
Posted
Updated 9-Nov-10 17:33pm
v2

Modify Sql Statement in Main()

Sql= "INSERT INTO T_CLOB(GUID,MESSAGES,INPUT_DATE) VALUES('" + guid + "' , :message, sysdate)";


Change InsertClob Method
OracleCommand command = new OracleCommand(SQLStatement, connOracle)
 command.Parameters.Add(":message", OracleDbType.Clob, messageClobValue, ParameterDirection.Input);

command.ExecuteNonQuery();


try it once. and also have a look on this
http://dotnetchris.wordpress.com/2008/04/10/writting-lobs-clobs-to-oracle-using-c-aspnet-20[^]

http://msdn.microsoft.com/en-us/library/cydxhzhz(VS.80).asp[^]
 
Share this answer
 
Hi All,

I am trying to insert xml to a Clob via C#,SQL/Oracle , I tried this code and many other version , is there any example that someone has tested it itself and works properly to post here?

I really appreciate your help if someone could help me regarding this.

Thanks in advance
 
Share this answer
 
Comments
Teamsar Muliadi 12-Jan-11 2:39am    
You can try this one :

public void DoInsertDataIncomingMsg(object sender)
{
OracleConnection objCon = new OracleConnection(CReaderConfig.StructureAppSettings.connStringAkses);
try
{
objCon.Open();
oTransc = objCon.BeginTransaction(IsolationLevel.Unspecified);
//oTransc.IsolationLevel = IsolationLevel.ReadCommitted;
oCommand = objCon.CreateCommand();
oCommand.Transaction = oTransc;
if (oCommand.Transaction == null)
{
log.Debug("Transaction 1 is null");
}
oCommand.CommandText = CQuery.Q_INSERT_INTO_T_INCOMING_MSGS(((CCommonStruct)sender).incoming_date,
((CCommonStruct)sender).membercode, ((CCommonStruct)sender).status_code,
((CCommonStruct)sender).getReferenceID);
oCommand.ExecuteScalar();

byte[] newvalue = System.Text.Encoding.Unicode.GetBytes(((CCommonStruct)sender).get_clob_messages);
log.Debug("New value : " + System.Text.Encoding.Unicode.GetString(newvalue));
log.Debug("Reference ID : " + ((CCommonStruct)sender).getReferenceID);

oCommand.CommandText = CQuery.Q_UPDATE_T_INCOMING_MSGS(((CCommonStruct)sender).getReferenceID);
if (oCommand.Transaction == null)
{
log.Debug("Transaction 2 is null");
}
using (OracleDataReader reader = oCommand.ExecuteReader())
{
reader.Read();
global::System.Data.OracleClient.OracleLob clob = reader.GetOracleLob(0);
clob.Write(newvalue, 0, newvalue.Length);
}
oTransc.Commit();
}
catch (Exception ex)
{
oTransc.Rollback();
log.Debug("Error while executing DoInsertDataIncomingMsg for Reference ID : " + ((CCommonStruct)sender).getReferenceID, ex);
}
finally
{
if (objCon != null)
{
objCon.Close();
oTransc.Dispose();
}
}
}
There are 2 steps for inserting data to clob field,

1. Insert the record
2. Open the same row with FOR UPDATE clause and execute the above line of code

The problem is while inserting record don't ignore CLOB field, instead use EMPTY_CLOB() in Insert statement as value for CLOB field.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900