linq 数据插入 删除 更新
插入:
ProductDataContext pa = new ProductDataContext();
PandC pd = new PandC();
pd.CNAME="aaaa";
pd.PID=100;
pd.PNAME = "aaaa";
pa.PandC.InsertOnSubmit(pd);
pa.SubmitChanges();
----------或者
ProductDataContext pa = new ProductDataContext();
PandC pd = new PandC { CNAME = "bbb", PID = 100, PNAME = "abbbaaa" };
pa.PandC.InsertOnSubmit(pd);
pa.SubmitChanges();
PandC 为数据表名 也是类名 将该类实例化 传参数 再插入数据
删除:
ProductDataContext pa = new ProductDataContext();
var deleted=from pc in pa.PandC where pc.CNAME=="bbb" select pc;
foreach (var detial in deleted)
{
pa.PandC.DeleteOnSubmit(detial);
}
pa.SubmitChanges();
更新:
ProductDataContext pa = new ProductDataContext();
var deleted = from pc in pa.PandC where pc.CNAME == "bbbss" select pc;
foreach (var detial in deleted)
{
detial.CNAME = "asdasd";
}
pa.SubmitChanges();
或者直接执行
ProductDataContext pa = new ProductDataContext();
pa.ExecuteCommand("UPDATE PandC SET CNAME='DDDD' WHERE CNAME='aaaa'");