. NET using the Dao.net entity Class model to manipulate the database

Source: Internet
Author: User

First, the new project

Open vs2017, create a new project, named Orm1

Second, the new database

Open the SQL Server database, create a new database Orm1, and create a new table student.

Third, the new ADO Entity Data Model

Click New Connection here to create a new database connection. In fact, the server name is entered. On behalf of the local server, authentication selects the default Windows identity authentication.

Choose our well-created database Orm1.

Remember that the connection name is Orm1entities, and the code behind it needs to be written.

Here, remember Shing.

Click Done to OK, there may be a pop-up below the warning, the point is OK.

Finally, the following view appears, creating the Dao.net entity Type model successfully.

Now point VS build above, point rebuild project. After successful console input:

Iv. New aspx file

New aspx file WebForm1.aspx

<%@ Page Language="C #"AutoEventWireup="true"codebehind="WebForm1.aspx.cs"Inherits="Orm1. WebForm1" %><!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Headrunat= "Server"><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>    <title></title></Head><Body>    <formID= "Form1"runat= "Server">        <Div>School Number:<Asp:textboxID= "Sid"runat= "Server"></Asp:textbox>        </Div>         <Div>Name:<Asp:textboxID= "Sname"runat= "Server"></Asp:textbox>        </Div>         <Div>Age:<Asp:textboxID= "Sage"runat= "Server"></Asp:textbox>        </Div>        <Div>            <Asp:buttonID= "Add"runat= "Server"Text= "new"OnClick= "Add_click" />            <Asp:buttonID= "Select"runat= "Server"Text= "Query"OnClick= "Select_click" />            <Asp:buttonID= "Delete"runat= "Server"Text= "Delete"OnClick= "Delete_click" />            <Asp:buttonID= "Update"runat= "Server"Text= "Modify"OnClick= "Update_click" />        </Div>        <Div>            <Asp:gridviewID= "GridView1"runat= "Server"></Asp:gridview>        </Div>    </form></Body></HTML>

Well, now we can write in CS file using Dao.net's entity type model to manipulate the code of the database.

1. Show All
        void ShowAll ()        {            varnew  orm1entities ();             = db. Student.tolist ();            Gridview1.databind ();        }

Do you remember orm1entities? Did you see the new entity Class model?

2. Finding data
        protected void Select_click (object  sender, EventArgs e)        {            varnew  orm1entities ();             var item = db. Student.where (M = M.sid = = sid. Text). ToList ();             = item;            Gridview1.databind ();        }

where M is any character.

3. New data
        protected void Add_click (object  sender, EventArgs e)        {            varnew  orm1entities ();             var New Student            {                = sid. Text,                = sname. Text,                int. Parse (Sage. Text)            };            Db. Student.add (item);            Db. SaveChanges ();            ShowAll ();        }
4. Delete data
        protected voidDelete_click (Objectsender, EventArgs e) {            vardb =Neworm1entities (); varitem = db. Student.where (M = M.sid = =SID. Text).            FirstOrDefault (); if(Item! =NULL) {db.                Student.remove (item); Db.                SaveChanges ();            ShowAll (); }            Else{Response.Write ("no such user"); }        }
5. Modify the data
        protected voidUpdate_click (Objectsender, EventArgs e) {            vardb =Neworm1entities (); varitem = 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 ("no such user"); }        }

. NET using the Dao.net entity Class model to manipulate the database

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.