Search code examples
c#oracle-databasestored-proceduresclob

Split string in three parts by character length c#


I've got a question about splitting a string message in three parts depending on the length of characters. The reason is because my stored procedure won't take more than 32767 characters. (pl/sql payload) Therefore i would like to send three messages (three clobs) to the stored procedure which can append those messages and send it to a queue.

Which solution is the best if I've got a string message and I need to calculate it into three parts where the max length of the message can be 32.000 characters?

What the stored procedure need: (qname IN varchar2, i_clob1 IN clob, i_clob2 IN clob, i_clob3 IN clob)

And how to send it in three parts if the string message is less than 32.000 characters for the first part but I want to send it in three parts anyway?

Here is my code which take one message (i_clob).

 public void Enqueue(string queueName, string mess)
        {
            OracleCommand cmd = null;
            try
            {
                cmd = new OracleCommand("", m_Connection)
                    {
                        CommandText = m_InSpName,
                        CommandType = CommandType.StoredProcedure
                    };


                //add Aq queue name 
                OracleParameter qName = new OracleParameter("qname", OracleType.VarChar)
                    {
                        Direction = ParameterDirection.Input,
                        Value = queueName
                    };

                //add message to enqueue
                OracleParameter message = new OracleParameter("i_clob", OracleType.Clob)
                    {
                        Direction = ParameterDirection.Input
                    };
                mess = mess.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
                message.Value = mess;

                cmd.Parameters.Add(qName);
                cmd.Parameters.Add(message);

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //rethrow exception and make sure we clean up i.e. execute finally below
                throw new Exception("An error occurred trying to deliver the message to the queue", ex);
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }
        }

Solution

  • I understand that you are looking for something on these lines:

    string i_clob1 = "";
    string i_clob2 = "";
    string i_clob3 = "";
    if (message.Length >= 3 && message.Length <= 32000 * 3)
    {
        int lastStart = 2 * message.Length / 3;
        int lastLength = message.Length - lastStart;
        i_clob1 = message.Substring(0, message.Length / 3);
        i_clob2 = message.Substring(message.Length / 3, message.Length / 3);
        i_clob3 = message.Substring(lastStart, lastLength);
    }
    else if (message.Length < 3)
    {
        i_clob1 = message;
    }