I am using entity framework to insert details into the database where I have a column varchar(50). when I am trying to insert some value more than 50 length it gives me an error string or binary data would be truncated.So ., for safe side I just changed it to varchar(100).
我正在使用实体框架将细节插入数据库,其中有一个列varchar(50)。当我试图插入超过50长度的值时,它会给我一个错误字符串或二进制数据将被截断。为了安全起见,我把它改成了varchar(100)
Now can someone let me know is this the only solution to change the value in the Db or do we have any alternatives....... I read an article http://sqlblogcasts.com/blogs/danny/archive/2008/01/12/scuffling-with-string-or-binary-data-would-be-truncated.aspx
现在有人能让我知道这是改变Db值的唯一方法吗?或者我们有其他的替代方法……我读过一篇文章http://sqlblogcasts.com/blogs/danny/archive/2008/01/12/scuffling-with-string-or- bin- data- being truncated.aspx
But how can I use such things in c#. I appreciate any suggestions from you.........
但是在c#中我怎么使用这些东西呢?我很感激你给我的建议。
Thanks
谢谢
3 个解决方案
#1
2
Depending on what type of input field you're dealing with, using varchar(max) could be an option.
根据所处理的输入字段的类型,可以使用varchar(max)。
But as previously pointed out, it really boils down to what your business requirements are.
但是正如前面所指出的,它实际上可以归结为您的业务需求是什么。
#2
2
Well, first of all, obviously you cannot insert a string longer than 50 in a varchar(50).
首先,显然不能在varchar(50)中插入大于50的字符串。
So you have two options depending on your requirement:
根据你的要求,你有两种选择:
- change the database (as you have found out) and make sure that all code 'upstream' will be able to tackle the longer data
- 更改数据库(如您所发现的),并确保所有“上游”代码都能够处理更长的数据
- add some validations or restrict user input so that you will never get a string that is longer
- 添加一些验证或限制用户输入,这样您永远不会得到一个更长的字符串
Well, there's a third and that is cut off the string without telling the user but I would not do that.
还有第三个,它在没有告诉用户的情况下切断了字符串,但我不会这么做。
So it depends on your business requirement what to do. But I would not do any 'tricks' like in the article you suggested
这取决于你的业务需求。但我不会像你建议的那样做任何“把戏”。
#3
0
You should set the field length in the db to whatever is a reasonable length and you should prevent users from entering values that does not fit within that length.
应该将db中的字段长度设置为合理的长度,并且应该防止用户输入不适合该长度的值。
#1
2
Depending on what type of input field you're dealing with, using varchar(max) could be an option.
根据所处理的输入字段的类型,可以使用varchar(max)。
But as previously pointed out, it really boils down to what your business requirements are.
但是正如前面所指出的,它实际上可以归结为您的业务需求是什么。
#2
2
Well, first of all, obviously you cannot insert a string longer than 50 in a varchar(50).
首先,显然不能在varchar(50)中插入大于50的字符串。
So you have two options depending on your requirement:
根据你的要求,你有两种选择:
- change the database (as you have found out) and make sure that all code 'upstream' will be able to tackle the longer data
- 更改数据库(如您所发现的),并确保所有“上游”代码都能够处理更长的数据
- add some validations or restrict user input so that you will never get a string that is longer
- 添加一些验证或限制用户输入,这样您永远不会得到一个更长的字符串
Well, there's a third and that is cut off the string without telling the user but I would not do that.
还有第三个,它在没有告诉用户的情况下切断了字符串,但我不会这么做。
So it depends on your business requirement what to do. But I would not do any 'tricks' like in the article you suggested
这取决于你的业务需求。但我不会像你建议的那样做任何“把戏”。
#3
0
You should set the field length in the db to whatever is a reasonable length and you should prevent users from entering values that does not fit within that length.
应该将db中的字段长度设置为合理的长度,并且应该防止用户输入不适合该长度的值。