C#更新Oracle Clob字段

参考1:https://learn.microsoft.com/zh-cn/dotnet/framework/data/adonet/oracle-lobs

参考2:https://www.feiniaomy.com/post/95482.html

一、现象

Oralce Blob Clob 等类型无法直接使用update set XXX='xxx'的方式已进行更新。特别是加入字符超过4000就会报字符过长的错误

根据参考1和2总结出如下,最终成功更新Clob字段。partData字段大约3万行左右。

二、Code

                string connectString ="";
                OracleConnection conn = new OracleConnection(connectString);
                string strSql = "SELECT PART_DATA FROM SUPERMARKET_DATA WHERE IDENTIFIER='"+identifier + "' and IDENTIFIER_TYPE = 0 for update";
                OracleCommand cmd = conn.CreateCommand();
                cmd.Transaction = cmd.Connection.BeginTransaction();
                cmd.CommandText = strSql;
                OracleDataReader reader = cmd.ExecuteReader();
                using (reader)
                {
                    reader.Read();
                    Oracle.ManagedDataAccess.Types.OracleClob clob = reader.GetOracleClob(0);
                    clob.Value.ToString();
                    byte[] buffer = System.Text.Encoding.Unicode.GetBytes(partData.ToString());
                    clob.Write(buffer, 0, buffer.Length);
                }
                cmd.Transaction.Commit();
        

  

posted @ 2023-12-06 15:24  迷神图卷  阅读(139)  评论(0编辑  收藏  举报