ADO data Access Class query, property extension

Source: Internet
Author: User

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

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.