today, I first use the data access class to query all personnel information based on the previous personnel Management System.
Main program Code:
List<users> Ulist =NewUsersdata (). Select (); if(ulist.count >0)//determine If the containing statement {foreach(Users U1inchUlist)//Traverse{console.writeline (u1). UserName+" "+ u1. PassWord +" "+ u1. Nickname +" "+ u1. Sex +" "+ u1. Birthday +" "+u1. Nation); } }
Usersdata code in the data access class:
publicList<users>Select () {List<Users> list=NewList<users>(); Cmd.commandtext="SELECT * from Users"; Conn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); if(dr. Hasrows)//determine if data is included { while(dr. Read ()) {Users u=NewUsers (); U.username=dr["UserName"]. ToString (); U.password=dr["PassWord"]. ToString (); U.nickname=dr["Nickname"]. ToString (); U.sex= Convert.toboolean (dr["Sex"]); U.birthday=convert.todatetime (dr["Birthday"]); U.nation=dr["Nation"]. ToString (); List. ADD (u); }} Conn. Close (); returnlist; }
After that, I learned the contents of the property Extension.
A property extension is the display of code in a program as a character that a normal user can also read. This requires us to add a new property to the member variable in the entity class, which is only written on read, get{}, which is called in the main program, such as:
PrivateDateTime _birthday; /// <summary> ///Birthday/// </summary> publicDateTime Birthday {Get{return_birthday;} Set{_birthday =value;} } public stringBirthdaystr {Get{return_birthday.tostring ("yyyy mm month DD Day");} }
If you want to change the content displayed in different tables, such as: people management system in the national code to the national name (national Code Nation in the People information table users, while the national code Nationcode and the national name Nationname in the National information table nation), The code in the entity class users is:
Private string_nation; /// <summary> ///ethnic/// </summary> public stringNation {Get{return_nation;} Set{_nation =value;} } public stringNationname {Get{nationdata ndata=NewNationdata (); stringEnd = Ndata.Select(_nation); returnend; } }
The code in the data access class Nationdata Is:
public classNationdata {SqlConnection Conn=NULL; SqlCommand cmd=NULL; publicnationdata () {conn=NewSqlConnection ("server=.; database=data0720;user=sa;pwd=123;"); CMD=Conn. CreateCommand (); } public string Select(stringCode) { stringEnd ="< no >"; Cmd.commandtext="SELECT * from Nation where [email protected]"; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@a", code); Conn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); if(dr. Hasrows) {dr. Read (); End=dr["Nationname"]. ToString (); } Conn. Close (); returnend; } }
today, I also learned that through the card breakpoint, can be good to deal with the program of unknown bugs, and can be very accurate to find the location of the Bug. As we write functions, the less we write about the function of each function, the more abstract it is, the wider the scope of this function will be applied.
ADO data Access Class query, property extension