在向大家详细介绍Linq使用InsertOnSubmit方法之前,首先让大家了解下调用SubmitChanges,然后全面介绍Linq使用InsertOnSubmit方法。
简单形式
说明:new一个对象,Linq使用InsertOnSubmit方法将其加入到对应的集合中,使用SubmitChanges()提交到数据库。
NorthwindDataContext db = new NorthwindDataContext();
var newnewCustomer = new Customer
{
CustomerID = "MCSFT",
CompanyName = "Microsoft",
ContactName = "John Doe",
ContactTitle = "Sales Manager",
Address = "1 Microsoft Way",
City = "Redmond",
Region = "WA",
PostalCode = "98052",
Country = "USA",
Phone = "(425) 555-1234",
Fax = null
};
db.Customers.InsertOnSubmit(newCustomer);
db.SubmitChanges();
语句描述:Linq使用InsertOnSubmit方法将新客户添加到Customers表对象。调用SubmitChanges将此新Customer保存到数据库。