0

I want to insert string to oracle database as clob data type in c#. How can I do this? some codes for example:

  private static byte[] GetByteArr(string listParameter)
    {
        if (listParameter!=null)
        {
            return Encoding.Unicode.GetBytes(listParameter);
        }
        else
        {
            return Encoding.Unicode.GetBytes(string.Empty);
        }
    }
  byte[] toArr = GetByteArr((string)eMessageList[1]);
  emailParameters[1] = command.Parameters.Add("pEto",OracleDbType.Clob, toArr,ParameterDirection.Input);
  command.ExecuteNonQuery();

this code insert toArr to database as System.Byte[].

2
  • 2
    If it's a clob (rather than a blob) you shouldn't need to convert it to a byte array. You might need to convert it to a char[], but not a byte[]... Additionally, you haven't said anything about what happens with the code you've already provided. Commented May 17, 2013 at 10:55
  • I haven't said anything about what happens, because some comments isn't releated about my operation, it's about what I do. I just want to learn insert string to db as clob data type. Commented May 17, 2013 at 10:59

2 Answers 2

3

try this

byte[] newvalue = System.Text.Encoding.Unicode.GetBytes(mystring);
var clob = new OracleClob(db);
clob.Write(newvalue, 0, newvalue.Length);
Sign up to request clarification or add additional context in comments.

1 Comment

Works for me. One points to note the database connection passed to OracleClob must be open.
0

Although the question seems to be outdated, I want to share an example that worked for me.

My intention was to save a JSON string (with more than 32k characters) into a clob field.

This is what I did:

string JSON_string = JsonConvert.SerializeObject(SomeObject);
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
myCommand.Parameters.AddWithValue("", SomeAttribute);
myCommand.Parameters.AddWithValue("", SomeAttribute2);
myCommand.Parameters.AddWithValue("", SomeAttribute3);
myCommand.Parameters.AddWithValue("", JSON_string);

And then execute the command. I'm using our companies library to do that:

DataSet myDS = myUser.myLoginUser._MySpAppS.RunSQL("INSERT INTO MARS$T_BCSAVINGS (MASSNAHMEN_ID, USER_ID, AKTIV, HEBELDATEI) VALUES (?, ?, ?, ?);", myCommand.Parameters);

I'm saving the result in a DataSet only to check if the query was successful.

So basically what I did, is to handover the string to the OleDbCommand parameters list and executed the query with those parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.