Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete rows by LINQ to SQL

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to delete lines in LINQ to SQL. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

You can delete rows in the database by removing the corresponding LINQ to SQL object from its collection associated with the table. LINQ to SQL converts the changes to the appropriate SQL DELETE command.

Things to pay attention to when implementing LINQ to SQL to delete rows:

LINQ to SQL does not support and does not recognize cascading delete operations. If you want to delete rows in a table that has row constraints, you must complete one of the following tasks:

◆ sets the ON DELETE CASCADE rule in the foreign key constraint of the database.

◆ uses its own code to first delete the child objects that prevent the parent object from being deleted.

Otherwise, an exception is thrown. See the second code example later in this topic.

Description of line deletion for LINQ to SQL:

You can override the LINQ to SQL default methods for Insert, Update, and Delete database operations. For more information, see Custom insert, Update, and Delete Operations (LINQ to SQL). Developers using Visual Studio can use the object-relational designer to develop stored procedures for the same purpose. For more information, see object Relational designer (Omax R designer). The following steps assume that you have connected to the Northwind database through a valid DataContext. For more information, see how to: connect to a database (LINQ to SQL). Delete rows in the database query the rows in the database to delete. Call the DeleteOnSubmit method. Commit changes to the database.

Implement LINQ to SQL to delete a row instance:

This first code example queries the details of order 11000 in the database, marks those order details for deletion, and then commits these changes to the database.

/ / 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. }

In the second example, the purpose is to delete the order (No. 10250). The code first checks the OrderDetails table to see if the order to be deleted has children. If the order has subitems, mark the subitems as deleted first, and then mark the order as deleted. DataContext sets the correct order for actual deletions so that delete commands sent to the database comply with database constraints.

Implement LINQ to SQL to delete a row instance:

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 (); the above is how to delete the LINQ to SQL line shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report