将linq更新为sql datacontext [英] update linq to sql datacontext

查看:76
本文介绍了将linq更新为sql datacontext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有运行时异常
无法将类型为"System.Data.SqlTypes.SqlDecimal"的对象转换为类型为"System.String".



如何更正以下代码以获得结果?



there is run time exception
Unable to cast object of type ''System.Data.SqlTypes.SqlDecimal'' to type ''System.String''.



how can i correct below code to get my result?

ProductsDataContext db = new ProductsDataContext(); 
var matchedproduct = db.GetTable<product>().SingleOrDefault(p =>p.ProductID==productID);
 if (matchedproduct != null)  
{             
        products product=new products();
product.ProductName = txtpname.Text;
db.SubmitChanges()};

推荐答案

您传递给数据上下文的数据是本地计算机上的文件.即使此方法可行,将其从计算机中移出也会遇到问题.

通常,在构建linq to sql上下文时,会在app.config或web.config中创建一个连接字符串.您需要找到此连接字符串的名称,然后在创建数据上下文时引用该名称.

因此,如果您的配置名称为"MyDatabase",那么您将在构造函数中传递它.
Your data passed to your data context is a file on your local machine. Even if this works, you will end up with issues once you move it off your machine.

Typically there is a connection string created in your app.config or your web.config when you build your linq to sql context. You need to find the name of this connection string then reference THAT name in the creation of your data context.

So if your configuration name is "MyDatabase" then that is what you will pass in the constructor.


您将获得名为matchedproduct
的产品. 但您正在尝试更新product
你为什么不尝试
you get the product named matchedproduct
but you are trying to update product
why dont you try
matchedproduct.ProductName = txtpname.Text;
db.SubmitChanges(); 


sql compact不支持linq to sql.
sql compact is not supported for linq to sql.


这篇关于将linq更新为sql datacontext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆