欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

oracl clob 对象上的 EF 核心设置 ORA-01460:无法执行转换请求或转换请求不合理 - 如果使用 dapper,需要进行特殊处理

最编程 2024-03-25 08:24:07
...

不然插入数据库会报错

ORA-01460: 转换请求无法实施或不合理

[Column("xxx",TypeName="ClOB")]

oracl 源代码 特别注明

    public override DbParameter CreateParameter([NotNull] DbCommand command, [NotNull] string name, [CanBeNull] object value, bool? nullable = default(bool?))
    {
        //IL_0017: Unknown result type (might be due to invalid IL or missing references)
        //IL_001d: Expected O, but got Unknown
        Check.NotNull(command, "command");
        OracleParameter val = this.CreateParameter(command, name, value, nullable);
        int num = 32767;
        bool flag = false;
        if (command.Connection != null && command.Connection.State == ConnectionState.Open && command.Connection.ServerVersion != null)
        {
            flag = command.Connection.ServerVersion.StartsWith("12.1");
        }
        if ((_oracleSQLCompatibility != null && _oracleSQLCompatibility == "11") | flag)
        {
            num = 4000;
        }
        if (this.get_StoreType() == "CLOB")
        {
            if (((DbParameter)val).Direction == ParameterDirection.Input)
            {
                if (value == null || (value != null && value is string && ((string)value).Length * 4 > 32767))
                {
                    val.set_OracleDbType(105);
                }
                else if (value == null || (value != null && value is char[] && ((char[])value).Length * 4 > 32767))
                {
                    val.set_OracleDbType(105);
                }
            }
            else
            {
                val.set_OracleDbType(105);
            }
        }
        if (this.get_StoreType() == "XMLTYPE")
        {
            if (((DbParameter)val).Direction == ParameterDirection.Input)
            {
                if (value == null || (value != null && value is string && ((string)value).Length * 4 > num))
                {
                    val.set_OracleDbType(127);
                }
                else if (value == null || (value != null && value is char[] && ((char[])value).Length * 4 > num))
                {
                    val.set_OracleDbType(127);
                }
            }
            else
            {
                val.set_OracleDbType(127);
            }
        }
        else if (this.get_StoreType() == "NCLOB")
        {
            val.set_OracleDbType(116);
        }
        else if (this.get_StoreType().StartsWith("NVARCHAR2"))
        {
            val.set_OracleDbType(119);
        }
        return (DbParameter)val;
    }

dapper 对clob的特殊处理 需要在设置 参数化的时候 对参数值做特殊处理

     db..Execute(cmdText, new
            {
             xxx=new OracleClobParameter(xxx)
            });

 public class OracleClobParameter : SqlMapper.ICustomQueryParameter
    {
        private readonly string value;

        public OracleClobParameter(string value)
        {
            this.value = value;
        }

        public void AddParameter(IDbCommand command, string name)
        {

            // accesing the connection in open state.
            var clob = new Oracle.ManagedDataAccess.Types.OracleClob(command.Connection as Oracle.ManagedDataAccess.Client.OracleConnection);

            // It should be Unicode oracle throws an exception when
            // the length is not even.
            var bytes = System.Text.Encoding.Unicode.GetBytes(value);
            var length = System.Text.Encoding.Unicode.GetByteCount(value);

            int pos = 0;
            int chunkSize = 1024; // Oracle does not allow large chunks.

            while (pos < length)
            {
                chunkSize = chunkSize > (length - pos) ? chunkSize = length - pos : chunkSize;
                clob.Write(bytes, pos, chunkSize);
                pos += chunkSize;
            }

            var param = new Oracle.ManagedDataAccess.Client.OracleParameter(name, Oracle.ManagedDataAccess.Client.OracleDbType.Clob);
            param.Value = clob;

            command.Parameters.Add(param);
        }