0

当我尝试插入数据库时​​出现此错误。我已经检查了大小是否足够长以及属性是否正常。这是我的 SQL 布局。

schoolID   int (Primary Key)
schoolNaam varchar(50)
stad       varchar(30)

我的插入代码:

BLSchool blSchool = new BLSchool();
List<School> scholen = blSchool.GetAll();
School school = new School();
school.SchoolNaam = schoolnaam.ToString();
school.Stad = schoolstad.ToString();
int schoolID = blSchool.InsertSchool(school);
db.Schools.InsertOnSubmit(school);
db.SubmitChanges();
return school.SchoolID;
4

2 回答 2

0

不,您没有检查尺寸或至少没有正确检查。schoolnaam长度超过 50 个字符,或者超过schoolstad30 个。

为避免该错误,请使用以下代码:

school.SchoolNaam = new string(schoolnaam.ToString().Take(50).ToArray());
school.Stad = new string(schoolstad.ToString().Take(30).ToArray());

如果值更长,这将只占用前 50/30 个字符。

于 2012-12-16T08:39:28.717 回答
0

字符串太长,只是你还没发现问题。

可能您应该只增加列长度。A 根据varchar需要占用尽可能多的空间。它不会浪费空间来增加尺寸。

您可能还应该验证客户端上的字符串并向用户显示错误。

不要只是截断它,因为这会在不通知用户的情况下破坏数据。

于 2012-12-16T09:37:47.807 回答