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 ();
- }
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.
- // 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 ();
- }
- }
- }
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. (