LINQ to SQL additions and deletions (reprint)

Source: Internet
Author: User

Code

Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-->using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Web;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Using System.Data.Common;

Using System.Collections.Generic;

Namespace Firstlinq

{

public partial class _default:system.web.ui.page

{

NorthwindDataContext CTX = new NorthwindDataContext ("Data source=zhangshuqiang;initial catalog=northwind; Persist Security info=true; User Id=sa; Password=sa ");

protected void Page_Load (object sender, EventArgs e)

{

LINQ1 ();

LINQ2 ();

Linq3 ();

}

<summary>

Change and delete

</summary>

private void Linq3 ()

{

Increase

T_user User = new T_user ();

User. UserName = "big Weather";

User. PWD = "123456";

Ctx.t_User.InsertOnSubmit (User);//The previous method is add ();

CTx. SubmitChanges ();

Change

Refer to this syntax string s = (from A in CTX. Customers Select a). Single (a = a.ID = = 2);

T_user userupdate = ctx.t_User.SingleOrDefault (T_user = t_user.id = 2);//single is no different from Singleordefault

Userupdate.username = "big weather 1";

CTx. SubmitChanges ();

By deleting

T_user Userdelete = (from UserInfo in ctx.t_user where userinfo.id = = 1 Select userinfo). FirstOrDefault ();

if (userdelete! = null)

{

Ctx.t_User.DeleteOnSubmit (Userdelete);

CTx. SubmitChanges ();

}

}

<summary>

Familiarity with LINQ to SQL syntax

</summary>

private void Linq2 ()

{

Customers customer = new Customers ();

Execute normal SQL statements and query customerid= "ANATR" records

Ienumerable<customers> Customers = ctx. Executequery<customers> ("select * from Customers where customerid= ' Anatr '");

Customer = Customers. First ();

Response.Write (Customer. CustomerID);

Querying a single record using LINQ

var cus = from C in CTX. Customers where C.customerid.equals ("Anatr") select C;

Customer = cus. First ();

Response.Write (Customer.companyname);

Query result set, syntax: From temporary table name in table collection by the name of the temporary table. Field name upgrade select temporary table name

Gdvcustomers.datasource = from Cust in CTX. Customers where Cust. CustomerID! = "Anatr" by Cust.companyname descending select Cust;

Gdvcustomers.databind ();

}

<summary>

Familiarize yourself with the LINQ syntax, where variables are defined like JS, without declaring types.

</summary>

private void Linq1 ()

{

var age = 29;

var username = "Greatverve";

var userlist = new[] {"A", "B", "C"};

foreach (var user in userlist)

{

Response.Write (user);

}

}

}

}

Http://www.cnblogs.com/greatverve/archive/2010/05/13/1734236.html

LINQ to SQL additions and deletions (reprint)

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.