繁体   English   中英

linq to sql在多个表上插入

[英]linq to sql insert on multiple table

我想使用“ linq to sql”在两个表上插入一条记录
我写这段代码:

dataclass temp1=new dataclass(connectionstring);
dataclass temp2=new dataclass(connectionstring);
human t=new human();
t.name=textbox.text;
temp1.insertonsubmit(t);
temp1.submitchange();

int x=t.id;

car y=new car();
y.name=textbox2.text;
temp2.insertonsubmit(y);
temp2.submitchange();

但是我在这一行有一个错误:

temp2.insertonsubmit(y);

错误 :

字符串或二进制数据将被截断。该语句已终止

为什么会这样? 我该如何解决?

该错误是因为您试图将数据插入到小于要插入的数据的varchar / varbinary列中。

我的猜测是textbox2.text包含的字符多于car.name支持的字符。

基本上,仅当数据库列大小和提供的数据长度不匹配时,才会发生此错误“字符串或二进制数据将被截断。该语句已终止”。

因此,您可以检查数据库中y.name的列大小。 如果它小于您的数据长度,则增加它。

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM