Ado. Net (c)--Database Operation class

Source: Internet
Author: User

Manipulating Data classes

Avoid code reuse, too many objects, no division of labor Development

Use the object-oriented approach to optimize the way data is accessed, using the encapsulation class

Generally packaged into three classes:

1. Data connection classes provide data connection objects

A reference namespace is required: using System.Data.SqlClient;

Cases:

 Public classDbconnect {Private Static stringConnString ="server=.; Database=mydb;user=sa;pwd=123";  Public StaticSqlConnection Conn//use a static method to obtain a connection object through an object or method        {            Get{//read-only properties                return NewSqlConnection (connstring); }        }    }

2. Entity classes are based on tables in the database

For example, the Nation table builds a nation class.

Table name = class name ; encapsulated field = property inside the table

Cases:

 Public classNation {Private stringCode;  Public stringCode {Get{returnCode;} Set{Code =value;} }        Private stringname;  Public stringName {Get{returnname;} Set{name =value;} }    }

3. Data access class implementation of various operations on the table additions and deletions

A reference namespace is required: using System.Data.SqlClient;

Querying all data returns a collection that uses list< to write the type of data > Generic collection (widely used in the collection of stored objects)

Cases:

//The main implementation of the nation table of various operations (increase and deletion of check)     Public classNationda {PrivateSqlConnection _conn;//Connection Object        PrivateSqlCommand _cmd;//Command Object        PrivateSqlDataReader _dr;//Reader Object//constructs a method to initialize a Connection object command object         PublicNationda () {_conn= Dbconnect.conn;//initializing a Connection object_cmd = _conn. CreateCommand ();//initializing a Command object        }        //ways to add data         Public BOOLADD (stringCodestringname) {_cmd.commandtext="INSERT into Nation values (@code, @name)"; _cmd. Parameters.addwithvalue ("@code", code); _cmd. Parameters.addwithvalue ("@name", name); _conn.            Open (); intn =_cmd.            ExecuteNonQuery (); _conn.            Close (); if(N >0)            {                return true; }            Else            {                return false; }        }        //ways to query all data         PublicList<nation>Select () {_cmd.commandtext="SELECT * from Nation"; _conn.            Open (); _DR=_cmd.                       ExecuteReader (); //to define an empty collectionlist<nation> list =NewList<nation>(); if(_DR. HasRows) { while(_DR. Read ()) {//Create a Nation objectNation data =NewNation (); Data. Code= _dr[0].                    ToString (); Data. Name= _dr[1].                    ToString (); //throw it in the collection .list.                ADD (data); }} _conn.            Close (); returnlist; }        //method of query based on condition         PublicList<nation> Select (stringcode) {_cmd.commandtext="SELECT * from Nation where [email protected]"; _cmd.            Parameters.clear (); _cmd. Parameters.addwithvalue ("@code", code); _conn.            Open (); _DR=_cmd.            ExecuteReader (); //to define an empty collectionlist<nation> list =NewList<nation>(); if(_DR. HasRows) { while(_DR. Read ()) {//Create a Nation objectNation data =NewNation (); Data. Code= _dr[0].                    ToString (); Data. Name= _dr[1].                    ToString (); //throw it in the collection .list.                ADD (data); }} _conn.            Close (); returnlist; }        //Delete Method         Public BOOLDelete (stringcode) {_cmd.commandtext="Delete from Nation where [email protected]"; _cmd.            Parameters.clear (); _cmd. Parameters.addwithvalue ("@code", code); _conn.            Open (); intn =_cmd.            ExecuteNonQuery (); _conn.            Close (); if(N >0)            {                return true; }            Else            {                return false; }        }        //Modify Method         Public BOOLUpdate (stringCodestringname) {_cmd.commandtext="update Nation Set [email protected] where [email protected]"; _cmd.            Parameters.clear (); _cmd. Parameters.addwithvalue ("@code", code); _cmd. Parameters.addwithvalue ("@name", name); _conn.            Open (); intn =_cmd.            ExecuteNonQuery (); _conn.            Close (); if(N >0)            {                return true; }            Else            {                return false; }        }                 Public stringNationname (stringcode) {_cmd.commandtext="Select Name from Nation where [email protected]"; _cmd. Parameters.addwithvalue ("@code", code); _conn.            Open (); _DR=_cmd.            ExecuteReader (); if(_DR. HasRows) {_dr.                Read (); return_dr[0].            ToString (); }            Else            {                return "Han"; } _conn.        Close (); }    }
View Code

Viewing elements using the Foreach Traversal collection

※foreach cannot add or subtract any element, but can modify the data

Cases:

Console.WriteLine (" Please enter code:");             string code = console.readline ();            List<Nation> list = da. Select (code);             foreach inch list)            {                "--" + data. Name);            }

Ado. Net (c)--Database Operation class

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.