用PostGreSQL實現三層(複習)

來源:互聯網
上載者:User

標籤:datagridview   style   blog   http   io   ar   os   sp   for   

modal DAL,BLL都是類庫的形式

最終結果如下:

資料庫代碼:

-- 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 INTO Student values(‘老大‘,20,‘12121212‘)

 

一、先建立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; }    }}

二、sqlhelper(問題:我把Server=127.0.0.1;Port=5432;User Id=postgres;Password=123456;Database=STUDENT;卸載app.config裡面,卻不能像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 readonly 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>        /// 得到所有資料----modal        /// </summary>        /// <param name="sql">sql語句</param>        /// <param name="parameters">參數</param>        /// <returns>模型</returns>        public List<studentModal> getAllStudentInfo(string sql,params NpgsqlParameter[] parameters)        {            using(NpgsqlConnection 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);                    //從dataTable中讀取資料形成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"]);//需要處理為int                        student.TelePhone = dataTable.Rows[i]["TelePhone"].ToString();                        studentList.Add(student);                    }                    return studentList;                }            }        }        ////轉換為object或者為空白        //private object FromDBValue(this object obj)        //{        //    return obj == DBNull.Value ? null : obj;        //}        ///// <summary>        ///// 轉換為資料庫中的null值        ///// </summary>        ///// <param name="obj"></param>        ///// <returns></returns>        //private object ToDBValue(this object obj)        //{        //    return obj == null ? DBNull.Value : obj;        //}    }}

三、DAL

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Mono.Security;using StudentModal;namespace DAL{    public class GetStudentInfo    {        /// <summary>        /// 構建sql語句,然後得到資料        /// </summary>        string sql = "select * from ";        public List<studentModal> GetAllStudentInfoDAL(string dataTable)        {            StudentModal.studentHelper studentHelper = new studentHelper();            return studentHelper.getAllStudentInfo(sql+dataTable);        }    }}

四、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>        /// 從DAL中得到所需資料,供UI調用        /// </summary>        /// <returns></returns>        public List<studentModal> GetStudentListBLL()        {            DAL.GetStudentInfo studentInfo = new GetStudentInfo();            return studentInfo.GetAllStudentInfoDAL(dataTable);        }            }}

五、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);            }        }

用PostGreSQL實現三層(複習)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.