Add, query, update, and delete objects in. Net 3.5-dlinq (LINQ to SQL)

Source: Internet
Author: User
Step by step vs 2008 +. Net 3.5 (8)-add, query, update, and delete Object-oriented Data in dlinq to SQL

Author: webabcd

Introduction
Taking northwind as an example database, dlinq (LINQ to SQL) fully object-oriented addition, query, update, and deletion operations

Example
Sample. aspx

<% @ Page Language = "C #" masterpagefile = "~ /Site. Master "autoeventwireup =" true "codefile =" sample. aspx. cs"
Inherits = "linq_dlinq_sample" Title = "Object-Oriented addition, query, update, and deletion" %>

<Asp: Content ID = "content1" contentplaceholderid = "head" runat = "server">
</ASP: content>
<Asp: Content ID = "content2" contentplaceholderid = "contentplaceholder1" runat = "server">
<P>
Category name: <asp: textbox id = "txtcategoryname" runat = "server"> </ASP: textbox>
Category Description: <asp: textbox id = "txtdescription" runat = "server"> </ASP: textbox>

<Asp: button id = "btnadd" runat = "server" text = "add" onclick = "btnadd_click"/>
</P>
<Asp: gridview id = "gvcategory" runat = "server" datakeynames = "categoryid" onselectedindexchanged = "gvcategory_selectedindexchanged"
Onrowdeleting = "gvcategory_rowdeleting" onrowcancelingedit = "gvcategory_rowcancelingedit"
Onrowediting = "gvcategory_rowediting" onrowupdating = "gvcategory_rowupdating">
<Columns>
<Asp: commandfield showselectbutton = "true" showeditbutton = "true" showdeletebutton = "true">
</ASP: commandfield>
</Columns>
</ASP: gridview>
<Br/>
<Asp: detailsview id = "dvproduct" runat = "server" datakeynames = "productid">
</ASP: detailsview>
</ASP: content>

Sample. aspx. csusing system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. xml. LINQ;

Using Dal;

Public partial class linq_dlinq_sample: system. Web. UI. Page
{
// Instantiate a northwinddatacontext (datacontext)
Northwinddatacontext _ CTX = new northwinddatacontext ();

Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Bindcategory ();
}
}

Private void bindcategory ()
{
// The category attribute of the northwinddatacontext object is the category set.
VaR categories = _ CTX. categories;

Gvcategory. datasource = categories;
Gvcategory. databind ();
}

Protected void btnadd_click (Object sender, eventargs E)
{
// Instantiate a category
Categories c = new categories ();

// Set attributes of the category object
C. categoryname = txtcategoryname. text;
C. Description = txtdescription. text;

// Use the insertonsubmit () method of the northwinddatacontext object to add a category object
_ CTX. categories. insertonsubmit (C );

// Generate and execute the corresponding SQL command
_ CTX. submitchanges ();

Gvcategory. editindex =-1;
Bindcategory ();
}

Protected void gvcategory_selectedindexchanged (Object sender, eventargs E)
{
// Use the query syntax to obtain the product set
VaR products = from P in _ CTX. Products
Where p. categories. categoryid = (INT) gvcategory. selectedvalue
Select P;

Dvproduct. datasource = products;
Dvproduct. databind ();
}

Protected void gvcategory_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
// Use the single query operator to obtain the specified category object
Categories category = _ CTX. categories. Single (C => C. categoryid = (INT) gvcategory. datakeys [E. rowindex]. value );

// Use deleteonsubmit () to delete the specified category object in the category set of the northwinddatacontext object
_ CTX. categories. deleteonsubmit (category );

// Generate and execute the corresponding SQL command
_ CTX. submitchanges ();

Gvcategory. editindex =-1;
Bindcategory ();
}

Protected void gvcategory_rowupdating (Object sender, gridviewupdateeventargs E)
{
// Use the query syntax and single query operator to obtain the specified category object
Categories category = (from C in _ CTX. Categories
Where C. categoryid = (INT) gvcategory. datakeys [E. rowindex]. Value
Select c). Single ();

// Set attributes of the category object
Category. categoryname = (textbox) gvcategory. Rows [E. rowindex]. cells [2]. controls [0]). text;
Category. Description = (textbox) gvcategory. Rows [E. rowindex]. cells [3]. controls [0]). text;

// Generate and execute the corresponding SQL command
_ CTX. submitchanges ();

Gvcategory. editindex =-1;
Bindcategory ();
}

Protected void gvcategory_rowediting (Object sender, gridviewediteventargs E)
{
Gvcategory. editindex = E. neweditindex;
Bindcategory ();
}

Protected void gvcategory_rowcancelingedit (Object sender, gridviewcancelediteventargs E)
{
Gvcategory. editindex =-1;
Bindcategory ();
}
}

OK
[Download source code]

 

This article is from the "webabcd" blog, please be sure to keep this source http://webabcd.blog.51cto.com/1787395/345006

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.