我收到此运行时异常
Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.String'.
如何修复我的代码以获得没有异常的结果?
ProductsDataContext db = new ProductsDataContext();
var matchedproduct = db.GetTable<product>().SingleOrDefault(p =>p.ProductID==productID);
if (matchedproduct != null)
product.ProductName = txtpname.Text;
db.SubmitChanges();
最佳答案
如果您没有收到编译时错误,那是因为您的 dbml 没有准确描述数据库中的列。您的对象认为它是一个字符串,但在数据库中它显然是一个十进制。您应该在 dbml 编辑器中更新它。然后,当您设置产品名称时,您必须从文本中解析出十进制值。
关于Linq-to-SQL DataContext 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10562359/