简体   繁体   中英

“String or binary data would be truncated” in update statement

I have the following code:

glat += result.geometry.location.lat;
glong += result.geometry.location.lng;
gPostal += postalCode;    

sqlUpdateQuery = "update testlatlong set Lat =@lat,Long =@long where postalcode =@postal";
SqlCommand updateCommand = new SqlCommand(sqlUpdateQuery, sqlConn);
updateCommand.Parameters.Add("@lat", SqlDbType.NVarChar).Value = glat;
updateCommand.Parameters.Add("@long", SqlDbType.NVarChar).Value = glong;
updateCommand.Parameters.Add("@postal", SqlDbType.NVarChar).Value = gPostal;
updateCommand.ExecuteNonQuery();

I kept encountering the issue: "string or binary data would be truncated. The statement has been terminated". The column types for my lat and long are nvarchar(max) in my sql tables. What could be the issue in my case?

It is going to be these lines:

glat += result.geometry.location.lat;
glong += result.geometry.location.lng;
gPostal += postalCode;    

They just look wrong. All the code you posted look like they are in a loop, for which the postal code will keep growing due to +=, eventually being longer than the size of the column.

Even if the lat and lng are varchar(max), what's it doing incrementally adding to the column value?

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