Linq C#增刪改查

來源:互聯網
上載者:User

我們繼續講解LINQ to SQL語句,這篇我們來討論Insert/Update/Delete操作。這個在我們的程式中最為常用了。我們直接看例子。

Insert/Update/Delete操作插入(Insert)1.簡單形式

說明:new一個對象,使用InsertOnSubmit方法將其加入到對應的集合中,使用SubmitChanges()提交到資料庫。

NorthwindDataContext db = new NorthwindDataContext();var newCustomer = 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();

語句描述:使用InsertOnSubmit方法將新客戶添加到Customers 表對象。調用SubmitChanges 將此新Customer儲存到資料庫。

2.一對多關聯性

說明:Category與Product是一對多的關係,提交Category(一端)的資料時,LINQ to SQL會自動將Product(多端)的資料一起提交。

var newCategory = new Category{    CategoryName = "Widgets",    Description = "Widgets are the ……"};var newProduct = new Product{    ProductName = "Blue Widget",    UnitPrice = 34.56M,    Category = newCategory};db.Categories.InsertOnSubmit(newCategory);db.SubmitChanges();

語句描述:使用InsertOnSubmit方法將新類別添加到Categories表中,並將新Product對象添加到與此新Category有外鍵關係的Products表中。調用SubmitChanges將這些新對象及其關係儲存到資料庫。

3.多對多關係

說明:在多對多關係中,我們需要依次提交。

var newEmployee = new Employee{    FirstName = "Kira",    LastName = "Smith"};var newTerritory = new Territory{    TerritoryID = "12345",    TerritoryDescription = "Anytown",    Region = db.Regions.First()};var newEmployeeTerritory = new EmployeeTerritory{    Employee = newEmployee,    Territory = newTerritory};db.Employees.InsertOnSubmit(newEmployee);db.Territories.InsertOnSubmit(newTerritory);db.EmployeeTerritories.InsertOnSubmit(newEmployeeTerritory);db.SubmitChanges();

語句描述:使用InsertOnSubmit方法將新僱員添加到Employees 表中,將新Territory添加到Territories表中,並將新EmployeeTerritory對象添加到與此新Employee對象和新Territory對象有外鍵關係的EmployeeTerritories表中。調用SubmitChanges將這些新對象及其關係保持到資料庫。

4.使用動態CUD重寫(Override using Dynamic CUD)

說明:CUD就是Create、Update、Delete的縮寫。下面的例子就是建立一個ID(主鍵)為32的Region,不考慮資料庫中有沒有ID為32的資料,如果有則替換原來的資料,沒有則插入。

Region nwRegion = new Region(){    RegionID = 32,    RegionDescription = "Rainy"};db.Regions.InsertOnSubmit(nwRegion);db.SubmitChanges();

語句描述:使用DataContext提供的分部方法InsertRegion插入一個地區。對SubmitChanges 的調用調用InsertRegion 重寫,後者使用動態CUD運行Linq To SQL產生的預設SQL查詢。

更新(Update)

說明:更新操作,先擷取對象,進行修改操作之後,直接調用SubmitChanges()方法即可提交。注意,這裡是在同一個DataContext中,對於不同的DataContex看下面的講解。

1.簡單形式
Customer cust =    db.Customers.First(c => c.CustomerID == "ALFKI");cust.ContactTitle = "Vice President";db.SubmitChanges();

語句描述:使用SubmitChanges將對檢索到的一個Customer對象做出的更新保持回資料庫。

2.多項更改
var q = from p in db.Products        where p.CategoryID == 1        select p;foreach (var p in q){    p.UnitPrice += 1.00M;}db.SubmitChanges();

語句描述:使用SubmitChanges將對檢索到的進行的更新保持回資料庫。

刪除(Delete)1.簡單形式

說明:調用DeleteOnSubmit方法即可。

OrderDetail orderDetail =    db.OrderDetails.First    (c => c.OrderID == 10255 && c.ProductID == 36);db.OrderDetails.DeleteOnSubmit(orderDetail);db.SubmitChanges();

語句描述:使用DeleteOnSubmit方法從OrderDetail 表中刪除OrderDetail對象。調用SubmitChanges 將此刪除保持到資料庫。

2.一對多關聯性

說明:Order與OrderDetail是一對多關聯性,首先DeleteOnSubmit其OrderDetail(多端),其次DeleteOnSubmit其Order(一端)。因為一端是主鍵。

var orderDetails =    from o in db.OrderDetails    where o.Order.CustomerID == "WARTH" &&    o.Order.EmployeeID == 3    select o;var order =    (from o in db.Orders     where o.CustomerID == "WARTH" && o.EmployeeID == 3     select o).First();foreach (OrderDetail od in orderDetails){    db.OrderDetails.DeleteOnSubmit(od);}db.Orders.DeleteOnSubmit(order);db.SubmitChanges();

語句描述語句描述:使用DeleteOnSubmit方法從Order 和Order Details表中刪除Order和Order Detail對象。首先從Order Details刪除,然後從Orders刪除。調用SubmitChanges將此刪除保持到資料庫。

3.推理刪除(Inferred Delete)

說明:Order與OrderDetail是一對多關聯性,在上面的例子,我們全部刪除CustomerID為WARTH和EmployeeID為3 的資料,那麼我們不須全部刪除呢?例如Order的OrderID為10248的OrderDetail有很多,但是我們只要刪除ProductID為11的OrderDetail。這時就用Remove方法。

Order order = db.Orders.First(x => x.OrderID == 10248);OrderDetail od =     order.OrderDetails.First(d => d.ProductID == 11);order.OrderDetails.Remove(od);db.SubmitChanges();

語句描述語句描述:這個例子說明在實體物件的引用實體將該對象從其EntitySet 中移除時,推理刪除如何導致在該對象上發生實際的刪除操作。僅當實體的關聯映射將DeleteOnNull設定為true且CanBeNull 為false 時,才會發生推理刪除行為。

使用Attach更新(Update with Attach)

說明:在對於在不同的DataContext之間,使用Attach方法來更新資料。例如在一個名為tempdb的NorthwindDataContext中,查詢出Customer和Order,在另一個NorthwindDataContext中,Customer的地址更新為123 First Ave,Order的CustomerID 更新為CHOPS。

//通常,通過從其他層還原序列化 XML 來擷取要附加的實體//不支援將實體從一個DataContext附加到另一個DataContext//因此若要複製還原序列化實體的操作,將在此處重新建立這些實體Customer c1;List<Order> deserializedOrders = new List<Order>();Customer deserializedC1;using (NorthwindDataContext tempdb = new NorthwindDataContext()){    c1 = tempdb.Customers.Single(c => c.CustomerID == "ALFKI");    deserializedC1 = new Customer    {        Address = c1.Address,        City = c1.City,        CompanyName = c1.CompanyName,        ContactName = c1.ContactName,        ContactTitle = c1.ContactTitle,        Country = c1.Country,        CustomerID = c1.CustomerID,        Fax = c1.Fax,        Phone = c1.Phone,        PostalCode = c1.PostalCode,        Region = c1.Region    };    Customer tempcust =        tempdb.Customers.Single(c => c.CustomerID == "ANTON");    foreach (Order o in tempcust.Orders)    {        deserializedOrders.Add(new Order        {            CustomerID = o.CustomerID,            EmployeeID = o.EmployeeID,            Freight = o.Freight,            OrderDate = o.OrderDate,            OrderID = o.OrderID,            RequiredDate = o.RequiredDate,            ShipAddress = o.ShipAddress,            ShipCity = o.ShipCity,            ShipName = o.ShipName,            ShipCountry = o.ShipCountry,            ShippedDate = o.ShippedDate,            ShipPostalCode = o.ShipPostalCode,            ShipRegion = o.ShipRegion,            ShipVia = o.ShipVia        });    }}using (NorthwindDataContext db2 = new NorthwindDataContext()){    //將第一個實體附加到當前資料內容,以跟蹤更改    //對Customer更新,不能寫錯    db2.Customers.Attach(deserializedC1);    //更改所跟蹤的實體    deserializedC1.Address = "123 First Ave";    //附加訂單列表中的所有實體    db2.Orders.AttachAll(deserializedOrders);    //將訂單更新為屬於其他客戶    foreach (Order o in deserializedOrders)    {        o.CustomerID = "CHOPS";    }    //在當前資料內容中提交更改    db2.SubmitChanges();}

語句描述:從另一個層中擷取實體,使用Attach和AttachAll將還原序列化後的實體附加到資料內容,然後更新實體。更改被提交到資料庫。

使用Attach更新和刪除(Update and Delete with Attach)

說明:在不同的DataContext中,實現插入、更新、刪除。看下面的一個例子:

//通常,通過從其他層還原序列化XML擷取要附加的實體//此樣本使用 LoadWith 在一個查詢中積極式載入客戶和訂單,//並禁用消極式載入Customer cust = null;using (NorthwindDataContext tempdb = new NorthwindDataContext()){    DataLoadOptions shape = new DataLoadOptions();    shape.LoadWith<Customer>(c => c.Orders);    //載入第一個客戶實體及其訂單    tempdb.LoadOptions = shape;    tempdb.DeferredLoadingEnabled = false;    cust = tempdb.Customers.First(x => x.CustomerID == "ALFKI");}Order orderA = cust.Orders.First();Order orderB = cust.Orders.First(x => x.OrderID > orderA.OrderID);using (NorthwindDataContext db2 = new NorthwindDataContext()){    //將第一個實體附加到當前資料內容,以跟蹤更改    db2.Customers.Attach(cust);    //附加相關訂單以進行跟蹤; 否則將在提交時插入它們    db2.Orders.AttachAll(cust.Orders.ToList());    //更新客戶的Phone.    cust.Phone = "2345 5436";    //更新第一個訂單OrderA的ShipCity.    orderA.ShipCity = "Redmond";    //移除第二個訂單OrderB.    cust.Orders.Remove(orderB);    //添加一個新的訂單Order到客戶Customer中.    Order orderC = new Order() { ShipCity = "New York" };    cust.Orders.Add(orderC);    //提交執行    db2.SubmitChanges();}

語句描述:從一個上下文提取實體,並使用 Attach 和 AttachAll 附加來自其他內容相關的實體,然後更新這兩個實體,刪除一個實體,添加另一個實體。更改被提交到資料庫。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.