詳解實(shí)現(xiàn)LINQ to SQL刪除行
實(shí)現(xiàn)LINQ to SQL刪除行是用到什么方法呢?具體的步驟又是什么呢?那么我們下面來(lái)具體看看在數(shù)據(jù)庫(kù)中如何實(shí)現(xiàn)LINQ to SQL刪除行的操作。希望對(duì)你了解和掌握這個(gè)技術(shù)有所幫助。
可以通過將對(duì)應(yīng)的 LINQ to SQL對(duì)象從其與表相關(guān)的集合中刪除來(lái)刪除數(shù)據(jù)庫(kù)中的行。LINQ to SQL 會(huì)將更改轉(zhuǎn)換為相應(yīng)的 SQL DELETE 命令。
實(shí)現(xiàn)LINQ to SQL刪除行要注意的事項(xiàng):
LINQ to SQL不支持且無(wú)法識(shí)別級(jí)聯(lián)刪除操作。如果要在對(duì)行有約束的表中刪除行,則必須完成以下任務(wù)之一:
◆在數(shù)據(jù)庫(kù)的外鍵約束中設(shè)置 ON DELETE CASCADE 規(guī)則。
◆使用自己的代碼首先刪除阻止刪除父對(duì)象的子對(duì)象。
否則會(huì)引發(fā)異常。請(qǐng)參見本主題中后面的第二個(gè)代碼示例。
實(shí)現(xiàn)LINQ to SQL刪除行說(shuō)明:
您可以重寫 Insert、Update 和 Delete 數(shù)據(jù)庫(kù)操作的 LINQ to SQL默認(rèn)方法。有關(guān)更多信息,請(qǐng)參見 自定義插入、更新和刪除操作 (LINQ to SQL)。使用 Visual Studio 的開發(fā)人員可以使用 對(duì)象關(guān)系設(shè)計(jì)器 來(lái)開發(fā)用于實(shí)現(xiàn)相同目的的存儲(chǔ)過程。有關(guān)更多信息,請(qǐng)參見 對(duì)象關(guān)系設(shè)計(jì)器(O/R 設(shè)計(jì)器).以下步驟假定您已通過有效的 DataContext 連接到 Northwind 數(shù)據(jù)庫(kù)。有關(guān)更多信息,請(qǐng)參見 如何:連接到數(shù)據(jù)庫(kù) (LINQ to SQL)。刪除數(shù)據(jù)庫(kù)中的行查詢數(shù)據(jù)庫(kù)中要?jiǎng)h除的行。調(diào)用 DeleteOnSubmit 方法。將更改提交到數(shù)據(jù)庫(kù)。
實(shí)現(xiàn)LINQ to SQL刪除行實(shí)例:
這***個(gè)代碼示例查詢數(shù)據(jù)庫(kù)中 11000 號(hào)訂單的詳細(xì)信息,將這些訂單詳細(xì)信息標(biāo)記為刪除,然后將這些更改提交到數(shù)據(jù)庫(kù)。
- // Query the database for the rows to be deleted.
 - var deleteOrderDetails =
 - from details in db.OrderDetails
 - where details.OrderID == 11000
 - select details;
 - foreach (var detail in deleteOrderDetails)
 - {
 - db.OrderDetails.DeleteOnSubmit(detail);
 - }
 - try
 - {
 - db.SubmitChanges();
 - }
 - catch (Exception e)
 - {
 - Console.WriteLine(e);
 - // Provide for exceptions.
 - }
 
在第二個(gè)示例中,目的是刪除訂單(10250 號(hào))。代碼首先檢查 OrderDetails 表以查看要?jiǎng)h除的訂單是否有子項(xiàng)。如果訂單有子項(xiàng),則首先將子項(xiàng)標(biāo)為刪除,然后將訂單標(biāo)為刪除。 DataContext 為實(shí)際刪除設(shè)置正確的順序,以使發(fā)送到數(shù)據(jù)庫(kù)的刪除命令遵守?cái)?shù)據(jù)庫(kù)約束。
實(shí)現(xiàn)LINQ to SQL刪除行實(shí)例:
- Northwnd db = new Northwnd(@"c:\northwnd.mdf");
 - db.Log = Console.Out;
 - // Specify order to be removed from database
 - int reqOrder = 10250;
 - // Fetch OrderDetails for requested order.
 - var ordDetailQuery =
 - from odq in db.OrderDetails
 - where odq.OrderID == reqOrder
 - select odq;
 - foreach (var selectedDetail in ordDetailQuery)
 - {
 - Console.WriteLine(selectedDetail.Product.ProductID);
 - db.OrderDetails.DeleteOnSubmit(selectedDetail);
 - }
 - // Display progress.
 - Console.WriteLine("detail section finished.");
 - Console.ReadLine();
 - // Determine from Detail collection whether parent exists.
 - if (ordDetailQuery.Any())
 - {
 - Console.WriteLine(
 - "The parent is presesnt in the Orders collection.");
 - // Fetch Order.
 - try
 - {
 - var ordFetch =
 - (from ofetch in db.Orders
 - where ofetch.OrderID == reqOrder
 - select ofetch).First();
 - db.Orders.DeleteOnSubmit(ordFetch);
 - Console.WriteLine(
 - "{0} OrderID is marked for deletion.",
 - ordFetch.OrderID);
 - }
 - catch (Exception e)
 - {
 - Console.WriteLine(e.Message);
 - Console.ReadLine();
 - }
 - }
 - else
 - {
 - Console.WriteLine(
 - "There was no parent in the Orders collection.");
 - }
 - // Display progress.
 - Console.WriteLine("Order section finished.");
 - Console.ReadLine();
 - try
 - {
 - db.SubmitChanges();
 - }
 - catch (Exception e)
 - {
 - Console.WriteLine(e.Message);
 - Console.ReadLine();
 - }
 - // Display progress.
 - Console.WriteLine("Submit finished.");
 - Console.ReadLine();
 
實(shí)現(xiàn)LINQ to SQL刪除行的操作基本內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)掌握實(shí)現(xiàn)LINQ to SQL刪除行有所幫助。
【編輯推薦】















 
 
 
 
 
 
 