ASP. NET uses LINQ to SQL to implement basic addition, deletion, modification, query, and binding controls (C #)

Source: Internet
Author: User

Just a little bit of LINQ was attracted by it, and I suddenly came up with the idea of learning and using it in one breath. So I posted a video on the ASP. NET official website. I am not very good at English, but I can understand how to use it. Below I wrote a little bit of fur I learned. It is a learning record.

First, I use Visual Studio 2008 development tools and SQL server data sources, which are written in C.

Step 1: Create an SQL Server Data Source

Create a database first

    

Another user table is created.

    

I believe these are all pediatric things, so I will not talk about them any more.

Step 2: Add a LINQ to SQL class

I created a user. dbml LINQ to SQL class.

    

Drag the table from the server resource manager to the view. You can double-click the table title in the view to rename it.

    

Save it.

Step 3: Create and execute a query

Namespace Test
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
BIND ();
}
}

Private void BIND ()
{
Userdatacontext DB = new userdatacontext (); // it is essential to create a datacontext class object.
VaR user = from u in db. myuser // rename it as myuser above. The original table name is user
Select U; // the query here is similar to SQL
Gridview1.datasource = user; // bind the data source

Gridview1.databind ();
}

// Insert information

Protected void btnadd_click (Object sender, eventargs E)
{
Userdatacontext DB = new userdatacontext ();
String username = textbox1.text;
String userpwd = textbox2.text;
// DB. myuser. insertonsubmit (New myuser {name = username, Pwd = userpwd });

// Both the above sentence and the following sentence can be implemented, but they are two different methods.
VaR user = new myuser {name = username, Pwd = userpwd };
DB. myuser. insertonsubmit (User); // execute insert
DB. submitchanges (); // submit changes to the database
BIND ();
}

The subsequent deletion and update operations are similar to the preceding insert operations, but the query and execution methods are different. The most important part is how to create a query. Later, we will provide you with some keywords for querying basic operations in LINQ. How to use these keywords remains to be learned in the future.

// Delete information

Protected void btndel_click (Object sender, eventargs E)
{
Userdatacontext DB = new userdatacontext ();
Int I = convert. toint32 (textbox3.text. Trim ());
VaR user = dB. myuser. First (P => P. Id. Equals (I ));
DB. myuser. deleteonsubmit (User );
DB. submitchanges ();
BIND ();
}

// Update information

Protected void btnupd_click (Object sender, eventargs E)
{
Userdatacontext DB = new userdatacontext ();
Int I = convert. toint32 (textbox3.text. Trim ());
VaR user = dB. myuser. First (P => P. Id. Equals (I ));
User. pwd + = user. pwd;
DB. submitchanges ();
BIND ();
}

}
}

 

* Query the keywords of basic operations in LINQ

-- From clause

-- Where clause

-- Select clause

-- Group clause

-- Into clause

-- Orderby clause

-- Join clause

-- Let clause

The above is a small article I first learned about LINQ. It may not be easy to understand. Please forgive me. If there is any error, please leave a message to me.

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.