c#-ade.net-entity classes, data access classes

Source: Internet
Author: User

entity classes, data access classes are evolved from encapsulation, making access to data easier, using only calls, and eliminating the need to write code again

Entity classes are a class that is encapsulated in the structure of a database table

First, the new folder App_Code, which holds class files such as database classes, creates a new class, for example: Users (with the same name as the database Access table) and Usersdata

Write database access Methods in class Usersdata

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data.SqlClient;namespaceThe lxc_ property extension. app_code{ Public classUsersdata {SqlConnection conn=NULL; SqlCommand cmd=NULL;  PublicUsersdata ()//The following database connection classes and database operation class statements are automatically instantiated when the main function is instantiated{conn=NewSqlConnection ("server=.; DATABASE=DATA0928;USER=SA;PWD=ASDF"); CMD=Conn.        CreateCommand (); }        /// <summary>        ///Adding data to the database if TRUE indicates that the add succeeds/// </summary>        /// <param name= "U" >the users object to be added to the database table</param>        /// <returns></returns>         Public BOOLInsert (Users u) {BOOLOK =false; Cmd.commandtext="INSERT into Users values (@a,@b,@c,@d,@e,@f,@g)"; Cmd.            Parameters.clear (); Cmd. Parameters.addwithvalue ("@a", U.username); Cmd. Parameters.addwithvalue ("@b", U.password); Cmd. Parameters.addwithvalue ("@c", U.nickname); Cmd. Parameters.addwithvalue ("@d", U.sex); Cmd. Parameters.addwithvalue ("@e", U.birthday); Cmd. Parameters.addwithvalue ("@f", u.nation); Cmd. Parameters.addwithvalue ("@g", U.class); intCount =0; Conn.            Open (); Count=cmd.            ExecuteNonQuery (); Conn.            Close (); if(Count >0) OK=true; returnOK; }        /// <summary>        ///Delete data from the database if True indicates that the deletion was successful/// </summary>        /// <param name= "username" ></param>        /// <returns></returns>         Public BOOLDelete (stringusername) {            BOOLOK =false; Cmd.commandtext="Delete from Users where [email protected]"; Cmd.            Parameters.clear (); Cmd. Parameters.addwithvalue ("@a", username); intCount =0; Conn.            Open (); Count=cmd.            ExecuteNonQuery (); Conn.            Close (); if(Count >0) OK=true; returnOK; }        /// <summary>        ///Modify the data in the database to indicate that the modification was successful if it returns True/// </summary>        /// <param name= "username" ></param>        /// <param name= "U" ></param>        /// <returns></returns>         Public BOOLUpdate (stringusername, Users u) {            BOOLOK =false; Cmd.commandtext="update Users Set [email protected],[email protected],[email protected],[email protected],[email protected],[email Protected],[email protected] where [email protected]"; Cmd.            Parameters.clear (); Cmd. Parameters.addwithvalue ("@a", U.username); Cmd. Parameters.addwithvalue ("@b", U.password); Cmd. Parameters.addwithvalue ("@c", U.nickname); Cmd. Parameters.addwithvalue ("@d", U.sex); Cmd. Parameters.addwithvalue ("@e", U.birthday); Cmd. Parameters.addwithvalue ("@f", u.nation); Cmd. Parameters.addwithvalue ("@g", U.class); Cmd. Parameters.addwithvalue ("@un", username); inti =0; Conn.            Open (); I=cmd.            ExecuteNonQuery (); Conn.            Close (); if(I >0) OK=true; returnOK; }        /// <summary>        ///queries all data in the database, returns the list<>Collection/// </summary>        /// <returns></returns>         PublicList<users>Select () {List<Users> list =NewList<users>(); Cmd.commandtext="Select *from Users"; Conn.            Open (); SqlDataReader Dr=cmd.            ExecuteReader (); if(Dr. HasRows) { while(Dr. Read ()) {Users U=NewUsers (); U.ids= (int) dr["IDs"]; U.username= dr["username"].                    ToString (); U.password= dr["Password"].                    ToString (); U.nickname= dr["Nickname"].                    ToString (); U.sex= (Boolean) dr["Sex"]; U.birthday= (DateTime) dr["Birthday"]; U.nation= dr["Nation"].                    ToString (); U.class= dr["class"].                    ToString (); List.                ADD (U); }} conn.            Close (); returnlist; }        /// <summary>        ///querying a data in a database, returning a list<>Collection/// </summary>        /// <param name= "username" >The user name of the data to query</param>        /// <returns></returns>         PublicList<users> Select (stringusername) {List<Users> list =NewList<users>(); Cmd.commandtext="Select *from Users where [email protected]"; Cmd.            Parameters.clear (); Cmd. Parameters.addwithvalue ("@a", username); Conn.            Open (); SqlDataReader Dr=cmd.            ExecuteReader (); if(Dr. hasrows) {Dr.                Read (); Users u=NewUsers (); U.ids= (int) dr["IDs"]; U.username= dr["username"].                ToString (); U.password= dr["Password"].                ToString (); U.nickname= dr["Nickname"].                ToString (); U.sex= (Boolean) dr["Sex"]; U.birthday= (DateTime) dr["Birthday"]; U.nation= dr["Nation"].                ToString (); U.class= dr["class"].                ToString (); List.            ADD (U); } conn.            Close (); returnlist; }    }}
data Access Classes-operation code

You can call the method in the main function

c#-ade.net-entity classes, data access classes

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.