Using PostgreSQL to achieve layer three (review)

Source: Internet
Author: User

Modal dal,bll are all forms of class libraries

The final result is as follows:

Database code:

--table:student--DROP Table student; CREATE TABLE Student (  name text NOT NULL,  ' number ' integer NOT NULL,  telephone text,  CONSTRAINT "primary Key "PRIMARY key (name))

Insert

INSERT into Student values (' eldest ', 20, ' 12121212 ')

First, establish modal

Using system;using system.collections.generic;using system.linq;using system.text;namespace StudentModal{    public class Studentmodal    {public        string Name {get; set;}        public int number {get; set;}        public string Telephone {get; set;}}    }

Second, SqlHelper (question: I put server=127.0.0.1; port=5432; User Id=postgres; password=123456;database=student; Uninstall app. config inside, but can't read it like MSSQL)

Using Npgsql;
Using system;using system.collections.generic;using system.configuration;using system.data;using System.Linq;using System.text;using mono.security;namespace studentmodal{public class Studenthelper {//private static Rea DOnly string constr = configurationmanager.connectionstrings["Consql"].        ToString (); private static readonly string constr = "server=127.0.0.1; port=5432; User Id=postgres;        Password=123456;database=student; ";        Private list<studentmodal> studentlist = new list<studentmodal> ();        private String sql = "SELECT * from Student"; <summary>//Get all data----modal///</summary>//<param name= "SQL" >sql statements </pa ram>//<param name= "parameters" > Parameters </param>//<returns> Model </returns> Pub Lic list<studentmodal> getallstudentinfo (String sql,params npgsqlparameter[] parameters) {using (N Pgsqlconnection con=new NpgsqlconnectiOn (constr)) {con.                Open (); using (npgsqlcommand cmd =new npgsqlcommand ()) {cmd.                    Connection = con;                    Cmd.commandtext = SQL; Cmd.                    Parameters.addrange (Parameters);                    Npgsqldataadapter adapter = new Npgsqldataadapter (cmd);                    DataSet DataSet = new DataSet (); Adapter.                    Fill (DataSet);                    Reading data from a DataTable to form modal DataTable datatable=dataset.tables[0];                    int tableRow = DataTable.Rows.Count; for (int i = 0; i < TableRow; i++) {Studentmodal student = new Studentmodal (                        ); Student. Name = datatable.rows[i]["Name"].                        ToString (); Student. Number =convert.toint32 (datatable.rows[i]["number"]);//needs to be handled as int student. Telephone = datatable.rows[i]["Telephone"].  ToString ();                      Studentlist.add (student);                } return studentlist;     }}}////converted to object or null//private object Fromdbvalue (This object obj)//{// return obj = = DBNull.Value?        Null:obj; }/////<summary>/////converted to null values in the database/////</summary>/////<param name= "obj        "></param>/////<returns></returns>//private Object Todbvalue (This object obj) {//return obj = = null?        DBNull.Value:obj; //}    }}

Third, DAL

Using system;using system.collections.generic;using system.linq;using system.text;using Mono.Security;using Studentmodal;namespace dal{public class Getstudentinfo {//<summary>///        Build SQL statements and get data //        </summary>        String sql = "SELECT * from";        Public list<studentmodal> Getallstudentinfodal (string dataTable)        {            Studentmodal.studenthelper Studenthelper = new Studenthelper ();            Return Studenthelper.getallstudentinfo (sql+datatable);}}    

Four, the BLL

Using dal;using studentmodal;using system;using system.collections.generic;using system.linq;using System.Text; namespace bll{public    class STUDENTBLL    {        private string dataTable = "Student";        <summary>//////        from the Dal to get the required data for UI calls//</summary>//        <returns></returns> Public        list<studentmodal> GETSTUDENTLISTBLL ()        {            DAL. Getstudentinfo studentinfo = new Getstudentinfo ();            Return Studentinfo.getallstudentinfodal (dataTable);}}            }

V. UI

        private void Button1_Click (object sender, EventArgs e)        {            list<studentmodal> studentlistbll = new list< Studentmodal> ();            Bll. STUDENTBLL STUDENTBLL = new BLL. STUDENTBLL ();            Studentlistbll= STUDENTBLL.GETSTUDENTLISTBLL ();            DATAGRIDVIEW1.ROWS.ADD (studentlistbll.count);            for (int j = 0; J < Studentlistbll.count; J + +)            {                Studentmodal studentmodal = studentlistbll[j];                DATAGRIDVIEW1.ROWS[J]. Cells[0]. Value = Studentmodal.name;                DATAGRIDVIEW1.ROWS[J]. CELLS[1]. Value = Studentmodal.number;                DATAGRIDVIEW1.ROWS[J]. CELLS[2]. Value = Studentmodal.telephone;                DATAGRIDVIEW1.ROWS.ADD (1);            }        }

Using PostgreSQL to achieve layer three (review)

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.