ADO database Access class query, property extension

Source: Internet
Author: User
Tags readable

1 database access class queries

(1) Query A

Parameters: Query The primary key of this message;

There is a return value: The return value is an entity class;

Dr.read (); Execute again, read this line of information and put it into the users type.

        Returns the users entity class type public        users Chayi (string name)        {            users u = null;            Com.commandtext = "Select *from users where [email protected]";            Com. Parameters.clear ();            Com. Parameters.Add ("@name", name);            Conn. Open ();            SqlDataReader dr = com. ExecuteReader ();            if (Dr. HasRows)///To determine if there is a message {///                Read this line of information and put it into the users type                Dr. Read ();                U = new users ();///instantiation of                u.uname = dr[0]. ToString ();                U.umima = Convert.ToInt32 (dr[1]. ToString ());                U.unicheng = dr[2]. ToString ();                U.usex = Convert.toboolean (dr[3]. ToString ());                U.ubir = Convert.todatetime (Dr[4]. ToString ());                U.uzu = dr[5]. ToString ();                        }            Conn. Close ();            return u;        }

(2) Check all

no parameters;

There is a return value: Put the returned value into the generic collection;

Dr.read (); After a bad read, every read, put this line of data into an entity class, and then put the entity class into the generic collection.

return type generic collection public        list<users> Chasuo ()         {        list<users> list=new list<users> ();/// Instantiating a generic collection        , users u = null;          Com.commandtext = "SELECT * from users";        Conn. Open ();        SqlDataReader dr = com. ExecuteReader ();        if (Dr. HasRows)///Determine if there is a message        {while            (Dr. Read ())//loop read, each row {//                /each row of data into the entity class                U = new users ();//Instantiate entity class                U.uname = Dr[0]. ToString ();                U.umima =convert.toint32 (dr[1]. ToString ());                U.unicheng = dr[2]. ToString ();                U.usex = Convert.toboolean (dr[3]. ToString ());                U.ubir =convert.todatetime (Dr[4]. ToString ());                U.uzu = dr[5]. ToString ();                List. ADD (U); Put an entity class into the generic collection                       }                }        conn. Close ();        return list;        }

Iterating through the collection displays all the information, one line at a time of each traversal.

            List<users> List1 = Ud.chasuo ();//Call query all functions            Console.WriteLine ("name" + "\ T" + "password" + "\ T" + "nickname" + "\ T" + "gender" + " \ t "+" date of birth "+" \ T "+" national ");            if (List1. count>0)            {            foreach (Users  U1 in List1)//Traversal collection            {                    Console.WriteLine (u1.uname + "\ T" + U1.umima + "\ T "+ U1.unicheng +" \ T "+ U1.usexget +" \ T "+ u1.ubirget+" \ T "+ u1.uzuget);}                        }

  

2 Property Extensions

(1) The member variable in the entity class is added with a read-only property, which allows the display to reach what we really want to display

Private BOOL_usex;//Private member variables Public BOOLproperties of the Usex//member variable (readable and writable) {Get{return_usex;} Set{_usex =value;} }         Public stringusexget//member variable properties (read-only) {Get{return_usex?"male":"female"; } To make the displayed content a"Male" or "female", not bool type.
}

(2) You can display the contents of a column with a foreign key relationship instead of a code name

A the foreign key joined table is written in two classes: entity class, data access class.

The data access class contains a query function: The parameter is the < outside keyword, and the return value is < What we need to display >.

B entity class Chinese and foreign keywords plus a read-only property

The query function for the table data access class that calls the foreign key join in the property, which can be changed to content when displayed.

Private string_uzu;//Private member variable Public stringuzu//property of member variable (readable writable) {Get{return_uzu;} Set{_uzu =value;} }         Public stringuzuget//member variable properties (read-only) {Get{mingzudata mz=NewMingzudata (); stringMN =Mz.cha (_uzu). mname;//calling a foreign key join table data Access class query functionreturnmn; }                }

ADO database 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.