简体   繁体   中英

linq to sql insert on multiple table

I want to insert a record on two tables using "linq to sql"
I write this code:

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();

But I have a error in this line:

temp2.insertonsubmit(y);

The Error :

string or binary data would be truncated.the statement has been terminated

Why do this happen? How can I solve do this?

The error is because you are trying to insert data into a varchar/varbinary column that is smaller than the data you wish to insert.

My guess would be that textbox2.text contains more characters than car.name supports.

Basically, this error "String or Binary data would be truncated. The statement has been terminated" will occur only if any mismatch between database column size and provided data length.

So can you check the column size of y.name in database. If it is less than your data length then increase it.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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