1, New project
Open vs2017 and create a new project named orm1
2, New database
Open SqlServer database, create new database orm1, and create new table student.
3, New ADO.NET entity data model
Click new connection here to create a new database connection. In fact, the server name input. Represents the local server, and the default Windows authentication is selected for authentication.
Select our created database, orm1.
Remember that the connection name here is or1entities, which is used to write the code later.
Check the form here
Click Finish to OK. The following warning may pop up. Click OK.
Finally, the following view appears, so far, the DAO.NET entity type model is created successfully.
Now click build above VS, and then click rebuild project. Console input after success:
4, New aspx file
New ASPX file webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="orm1.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> Student ID:<asp:TextBox ID="sid" runat="server"></asp:TextBox> </div> <div> full name:<asp:TextBox ID="sname" runat="server"></asp:TextBox> </div> <div> Age:<asp:TextBox ID="sage" runat="server"></asp:TextBox> </div> <div> <asp:Button ID="Add" runat="server" Text="Newly added" OnClick="Add_Click" /> <asp:Button ID="Select" runat="server" Text="query" OnClick="Select_Click" /> <asp:Button ID="Delete" runat="server" Text="delete" OnClick="Delete_Click" /> <asp:Button ID="Update" runat="server" Text="modify" OnClick="Update_Click" /> </div> <div> <asp:GridView ID="GridView1" runat="server"></asp:GridView> </div> </form> </body> </html>
Now, we can write the code to operate the database using the entity type model of Dao.NET in cs file.
1. Display all
void ShowAll() { var db = new orm1Entities(); GridView1.DataSource = db.Student.ToList(); GridView1.DataBind(); }
Remember ORM1 entities? Have you seen it when creating a new entity class model?
2. Find data
protected void Select_Click(object sender, EventArgs e) { var db = new orm1Entities(); var item = db.Student.Where(M => M.sid == sid.Text).ToList(); GridView1.DataSource = item; GridView1.DataBind(); }
where M is any character.
3. New data
protected void Add_Click(object sender, EventArgs e) { var db = new orm1Entities(); var item = new Student { sid = sid.Text, sname = sname.Text, sage = int.Parse(sage.Text) }; db.Student.Add(item); db.SaveChanges(); ShowAll(); }
4. Delete data
protected void Delete_Click(object sender, EventArgs e) { var db = new orm1Entities(); var item = db.Student.Where(M => M.sid == sid.Text).FirstOrDefault(); if (item != null) { db.Student.Remove(item); db.SaveChanges(); ShowAll(); } else { Response.Write("There is no such user"); } }
5. Modify data
protected void Update_Click(object sender, EventArgs e) { var db = new orm1Entities(); var item = db.Student.Where(M => M.sid == sid.Text).FirstOrDefault(); if (item != null) { item.sname = sname.Text; item.sage = int.Parse(sage.Text); db.SaveChanges(); ShowAll(); } else { Response.Write("There is no such user"); } }