通过nhibernate对列进行大的值,但对于oracle来说是蟾蜍可以(Value to large for column via nhibernate but ok in toad for ora

编程入门 行业动态 更新时间:2024-10-24 13:27:48
通过nhibernate对列进行大的值,但对于oracle来说是蟾蜍可以(Value to large for column via nhibernate but ok in toad for oracle)

给定Oracle表中的此字段:

ColumnName varchar(255 byte)

鉴于此字符串:

-- abcd abcdefghijkl mnokpqrstuvwxy abcd ab abcdefgh abc def abc abcdef ab.* abcd abcdefgh ij abcd abcdefghijkl mn ab ab.cd = ef.abcdefghijk ab ab.cd = ab.cdabe abcd abcdefghi.jklmnopqrstuvwxyzabc de ef ea.fd = ef.aezerfds azere fr.qsdfrtd = 1 fds ad.dfdsq

我们将这个文本插入到指定的列中,并带有一个简单的insert语句,借助于toad for oracle:它工作正常。

但是:如果我们在nhibernate的帮助下插入这个文本,我们将得到一个oracle错误:

ORA-12899: value too large for column [**SNIP**] (actual: 262, maximum: 255)

如果您对给定字符串执行字节计数,则此错误实际上是正确的,我们将获得262的计数。

为什么nhibernate会失败,为什么oracle for the success会成功进行相同的操作?

有没有人遇到过这个问题或与之相关的事情?

编辑

值得一提的是,该字符串已在html textarea中提交。 这里可能的问题是返回在浏览器中被计为1个字符,但它应该被计为超过1个字符,因为在.net中它由\ r \ n表示。

所有这些都在asp.net mvc场景中。

Given this field in an Oracle table:

ColumnName varchar(255 byte)

Given this string:

-- abcd abcdefghijkl mnokpqrstuvwxy abcd ab abcdefgh abc def abc abcdef ab.* abcd abcdefgh ij abcd abcdefghijkl mn ab ab.cd = ef.abcdefghijk ab ab.cd = ab.cdabe abcd abcdefghi.jklmnopqrstuvwxyzabc de ef ea.fd = ef.aezerfds azere fr.qsdfrtd = 1 fds ad.dfdsq

We insert this text into the specified column with a simple insert statement with the help of toad for oracle: that works fine.

However: if we insert this text with the help of nhibernate we will get an oracle error:

ORA-12899: value too large for column [**SNIP**] (actual: 262, maximum: 255)

This error is actually correct if you do a byte count of the given string, we will get a count of 262.

Why does nhibernate fail and why does toad for oracle succeed for the same action?

Has anyone any experience with this problem or something related to it?

Edit

Worth to mention here is that the string has been submitted in a html textarea. Possible problem here is that a return is counted as 1 character in the browser but it should be counted as more then 1 character since in .net it is represented by \r\n.

All this in a asp.net mvc scenario.

最满意答案

实际问题:

它与nhibernate或oracle无关。 当带有换行符的textarea被发布到asp.net mvc的“后端”时,我们将这些换行符称为\ r \ n 。 它计为2个字符,这导致数据库保存2个字节

但是在我们的情况下,我们有一个maxlength = 255 html属性。 浏览器将换行计为1个字符 。 看到不同?

解决方案

我们用\ n替换\ r \ n来解决它。

public class NewLineModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (result != null && !string.IsNullOrEmpty(result.AttemptedValue) && bindingContext.ModelType == typeof(string)) { var value = result.AttemptedValue; var replacedValue = value.Replace("\r\n", "\n"); return replacedValue; } return base.BindModel(controllerContext, bindingContext); } }

The actual problem:

It has nothing to do with nhibernate or oracle. When a textarea with newlines is posted to the "backend" of asp.net mvc we recieve those newlines as \r\n. It counts as 2 characters, which results in 2 bytes for the database save.

However in our situation we have a maxlength=255 html attribute. The browser counts a newline as 1 character. See the difference?

The solution

We solved it by replacing \r\n with \n.

public class NewLineModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (result != null && !string.IsNullOrEmpty(result.AttemptedValue) && bindingContext.ModelType == typeof(string)) { var value = result.AttemptedValue; var replacedValue = value.Replace("\r\n", "\n"); return replacedValue; } return base.BindModel(controllerContext, bindingContext); } }

更多推荐

oracle,nhibernate,字符串,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-08-07 19:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蟾蜍   说是   oracle   nhibernate   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!