Traversal query for C #
Yesterday our query can only query a piece of data, the query is only the student number is the number of the students to enter the information, today we look at all the information
In fact, the step is the same as yesterday, just need to put the query out of each object into the collection, in the program in one output to the line
This is our function, the return value returns a generic collection, and the data type is the type of our object, with no length.
Public list<xuesheng> Select1 ()
{
list<xuesheng> list = new list<xuesheng> ();//instantiation of the collection
Cmd.commandtext = "Select *from Xuesheng";//Start query
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
if (Dr. HasRows)//have data in
{
while (Dr. Read ())//Put the data inside the database into the object
{
Xuesheng xs = new Xuesheng ();
Xs. Code = Int. Parse (dr["code"). ToString ());
Xs. Name = dr["Name"]. ToString ();
Xs. sex = dr["Sex"]. ToString ();
Xs. Banji = dr["Banji"]. ToString ();
Xs. Birthday = DateTime.Parse (dr["Birthday"]. ToString ());
Xs. Nation = dr["Nation"]. ToString ();
List. Add (xs);//Put objects into the collection
}
}
Else
{
Console.WriteLine ("No information in Data Warehouse!");
}
Conn. Close ();
Return list;//Returns the collection
}
The rest is just going to walk through the set in program.
List<xuesheng> List = new list<xuesheng> ();//Instantiate collection
XUESHENGSJ XSS = new XUESHENGSJ ();
list = Xss.select1 ();
foreach (Xuesheng xs in list)
{
Console.WriteLine (XS. Code.tostring ()) + " " + XS. Name + " " + XS. Sex + " " + XS. Banji + " " + XS. Birthdaystr + " " + xs.nation);
}
Property extension
Simple attribute extensions can be written directly in the entity class.
Private DateTime _birthday;
Public DateTime Birthday
{
get {return _birthday;}
set {_birthday = value;}
}
public string birthdaystr//Property extension
{
get {return _birthday. ToString ("yyyy mm month DD day"); }
}
You need to re-write the data operation class to use the function.
private string _nation;
public string Nation
{
get {return _nation;}
set {_nation = value;}
}
public String nation//property extension, the following writes out the extended NATIONLGSJ function
{
Get
{
NATIONLGSJ NSJ = new NATIONLGSJ ();
String language = Nsj.select (_nation);
return language;
}
}
The function of the above property extension
public string Select (String Nation)
{
string language = null;
Cmd.commandtext = "Select *from minzu where[email protected]";//query for another table
Cmd. Parameters.clear ();
Cmd. Parameters.Add ("@nation", Nation);
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
if (Dr. HasRows)
{
Dr. Read ();
Language = dr["language"]. ToString ();//write out the language of the nation
}
Conn. Close ();
return language;
}
0914 traversal query for C #-property extension