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

  1. Namespace test
  2. {
  3. Public partial class _ Default: System. Web. UI. Page
  4. {
  5. Protected void Page_Load (object sender, EventArgs e)
  6. {
  7. If (! IsPostBack)
  8. {
  9. Bind ();
  10. }
  11. }
  12. Private void Bind ()
  13. {
  14. UserDataContext db = new UserDataContext (); // it is essential to create a DataContext class object.
  15. Var user = from u in db. MyUser // rename it as MyUser above. The original table name is User
  16. Select u; // the query here is similar to SQL
  17. GridView1.DataSource = user; // bind the data source
  18. GridView1.DataBind ();
  19. }
  20. // Insert information
  21. Protected void btnAdd_Click (object sender, EventArgs e)
  22. {
  23. UserDataContext db = new UserDataContext ();
  24. String userName = TextBox1.Text;
  25. String userPwd = TextBox2.Text;
  26. // Db. MyUser. InsertOnSubmit (new MyUser {Name = userName, Pwd = userPwd });
  27. // Both the above sentence and the following sentence can be implemented, but they are two different methods.
  28. Var user = new MyUser {Name = userName, Pwd = userPwd };
  29. Db. MyUser. InsertOnSubmit (user); // execute insert
  30. Db. SubmitChanges (); // submit changes to the database
  31. Bind ();
  32. }

Copy code

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.

  1. // Delete information
  2. Protected void btnDel_Click (object sender, EventArgs e)
  3. {
  4. UserDataContext db = new UserDataContext ();
  5. Int I = Convert. ToInt32 (TextBox3.Text. Trim ());
  6. Var user = db. MyUser. First (p => p. Id. Equals (I ));
  7. Db. MyUser. DeleteOnSubmit (user );
  8. Db. SubmitChanges ();
  9. Bind ();
  10. }
  11. // Update information
  12. Protected void btnUpd_Click (object sender, EventArgs e)
  13. {
  14. UserDataContext db = new UserDataContext ();
  15. Int I = Convert. ToInt32 (TextBox3.Text. Trim ());
  16. Var user = db. MyUser. First (p => p. Id. Equals (I ));
  17. User. Pwd + = user. Pwd;
  18. Db. SubmitChanges ();
  19. Bind ();
  20. }
  21. }
  22. }

Copy code

* 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. (

Related Article

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.