简体   繁体   中英

How to update Oracle CLOB column with long string using C# and Oracle Data Access Client

I'm trying to update a CLOB column in my database with a long string containing the HTML contents of an email. There are 18,000 characters in the record I'm having an issue with.

The below code will work if I set the html variable to "short string". But if I try to run the code with the long 18,000 character HTML string, I get this error: "Oracle.DataAccess.Client.OracleException ORA-22922: nonexistent LOB value ORA-02063: preceding line from ((servername))"

public static void UpdateHtmlClob(string html, string taxId,string un, string pw)
    {
        using (OracleConnection conn = new OracleConnection())
        {
            try
            {
                conn.ConnectionString = "User Id=" + un + ";Password=" + pw + ";Data Source=server.com;";
                conn.Open();


                OracleCommand cmd = new OracleCommand();
                string indata = html;
                cmd.CommandText = "UPDATE table1 SET HTML_BODY = :clobparam";
                OracleParameter clobparam = new OracleParameter("clobparam", OracleDbType.Clob, indata.Length);
                clobparam.Direction = ParameterDirection.Input;
                clobparam.Value = indata;
                cmd.Parameters.Add(clobparam);
                cmd.Connection = conn;
                cmd.ExecuteNonQuery();

                conn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                conn.Close();
            }
        }
    }

Before you edited your code to reflect my answer, there were two problems with your code that I saw.

Firstly, you need to use a colon in your command text to tell Oracle that clobparam is a bind variable, not a column name:

            cmd.CommandText = "UPDATE table1 SET HTML_BODY = :clobparam";

Secondly, you were not setting the database connection anywhere on the command. Which connection should the command be using? In your situation you have only one connection but more generally it may be possible to have more than one connection open. Add the line

            cmd.Connection = connection;

or alternatively create the command using

            OracleCommand cmd = connection.CreateCommand();

Of course, it would be nice if Oracle.DataAccess returned an error message that gave you the slightest hint that this was what you were doing wrong.

Anyway, now that you've edited your question to include the critical detail ORA-02063: preceding line from ((servername)) , which tells us that you are using a database link, all I can really do is echo what I wrote in the comment: connect direct to the remote database to transfer LOB data, don't use a database link.

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