0914 traversal query for C #-property extension

Source: Internet
Author: User

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

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.