使用Linq to SQL時﹐查詢是一件很方便的事


var
product = (from p in dataContext.Products
where p.ProductID == 1
select p).Single();


一行打死....

 

那更新資料庫中的資料呢?

 

var product = (from p in dataContext.Products
where p.ProductID == 1
select p).Single();

product.Name = "Richard’s product";


dataContext.SubmitChanges();

了不起多了幾行


問題︰可否不先做查詢而直接更新資料?
答︰你有二種方式。
1.使用ExecuteCommand去送出更新資料。
2.初始化一個Class﹐然後使用Attach method 將之加入監控。再將所有更新的值丟入。


var product = new Product();
product.ProductID = 1;

Products.Attach(product);


product.Name =
"Richard’s product";

SubmitChanges();

這樣就可以了。


如果﹐你發現這樣做並不會Update?

檢查你資料表的PK中的property裡的Time Stamp必需要為True

這樣就可以達到更新資料的功能了。




 

銀狐︰這個東西花了我快一天...真ox

arrow
arrow
    全站熱搜

    銀狐 發表在 痞客邦 留言(0) 人氣()