Data Access Class Query:
Public list<users> Select ()//query One piece of data
{
Generic collection, placing all users Data Objects
list<users> list = new List<users> ();
Cmd.commandtext = "select *from Users";
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
If (dr. hasrows)//if The data table has data
{
While (dr. Read ())//loop through all data
{
Create a users object for each row of data read
Users u = new users ();
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 ();
In the loop, each object you make is put into a collection
List. ADD (u);
}
}
Conn. Close ();
Return list;
}
public void Select1 ()//query all data
{
Cmd.commandtext = "select *from Users";
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
If (dr. hasrows)//if The data table has data
{
While (dr. Read ())//loop through all data
{
Console.WriteLine (dr["UserName"] + "" + dr["PassWord"]);
}
}
Conn. Close ();
}
Property Extensions:
Cases:
Private DateTime _birthday;
<summary>
Birthday
</summary>
Public DateTime Birthday//package
{
Get {return _birthday;}
Set {_birthday = value;}
}
public string birthdaystr//extended property, defined as String type
{
Get {return _birthday. ToString ("yy years mm DD Day")}//read only
}
ADO data Access Class query, property extension