简体   繁体   中英

How to convert string to oracle clob for insert database?

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[] .

try this

byte[] newvalue = System.Text.Encoding.Unicode.GetBytes(mystring);
var clob = new OracleClob(db);
clob.Write(newvalue, 0, newvalue.Length);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM