用PostGreSQL实现三层(复习)

简介: modal DAL,BLL都是类库的形式 最终结果如下: 数据库代码: -- Table: student -- DROP TABLE student; CREATE TABLE student ( name text NOT NULL, "number" int...

modal DAL,BLL都是类库的形式

最终结果如下:

image

数据库代码:

-- 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);
            }
        }
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
存储 运维 Cloud Native
【实操系列】基于AnalyticDB PostgreSQL数据共享实现企业级跨多业务的敏捷分析
云数据仓库AnalyticDB PostgreSQL 版发布了最新自研的云原生架构实例,实现了跨实例间的数据共享能力。允许进行跨实例间的实时数据共享且无需进行数据迁移,可支持构建安全、高效、灵活的数据分析场景。本文介绍了依托数据共享实现云数仓跨多业务实例的敏捷数据分析方案;
【实操系列】基于AnalyticDB PostgreSQL数据共享实现企业级跨多业务的敏捷分析
|
存储 SQL 关系型数据库
数据库误操作后悔药来了:AnalyticDB PostgreSQL教你实现分布式一致性备份恢复
本文将介绍AnalyticDB PostgreSQL版备份恢复的原理与使用方法。
1511 0
数据库误操作后悔药来了:AnalyticDB PostgreSQL教你实现分布式一致性备份恢复
|
SQL 存储 Oracle
PostgreSQL 事务隔离级别的实现和多版本并发控制|学习笔记
快速学习 PostgreSQL 事务隔离级别的实现和多版本并发控制
PostgreSQL 事务隔离级别的实现和多版本并发控制|学习笔记
|
关系型数据库 测试技术 PostgreSQL
postgresql实现影响分析
通过postgresql模仿分析假如城市发布通知,位于街道的人员是否受到了影响
441 0
postgresql实现影响分析
|
弹性计算 资源调度 运维
【实操系列】 AnalyticDB PostgreSQL发布实例计划管理功能,实现资源分时弹性&分时启停
本文将对AnalyticDB PostgreSQL产品的计划任务管理功能以及其背后的实现机制和最佳实践做详细介绍。
【实操系列】 AnalyticDB PostgreSQL发布实例计划管理功能,实现资源分时弹性&分时启停
|
存储 Kubernetes 负载均衡
「在 Kubernetes 上运行 Pgpool-Il」实现 PostgreSQL 查询(读)负载均衡和连接池
「在 Kubernetes 上运行 Pgpool-Il」实现 PostgreSQL 查询(读)负载均衡和连接池
676 0
「在 Kubernetes 上运行 Pgpool-Il」实现 PostgreSQL 查询(读)负载均衡和连接池
|
SQL 监控 关系型数据库
PostgreSQL Oracle 兼容性之 - performance insight - AWS performance insight 理念与实现解读 - 珍藏级
PostgreSQL , perf insight , 等待事件 , 采样 , 发现问题 , Oracle 兼容性
1304 0
|
SQL 安全 大数据
使用 Dataworks 实现 AnalyticDB PostgreSQL版上的 ETL 作业调度
DataWorks作为阿里云上广受欢迎的大数据开发调度服务,最近加入了对于AnalyticDB PostgreSQL版(原HybridDB for PostgreSQL,简称ADB PG版)的支持,意味着所有ADB PG版的客户可以获得任务开发、任务依赖关系管理、任务调度、任务运维.
3356 0
|
人工智能 关系型数据库 MySQL
如何将PostgreSQL与MySQL相结合,实现1+1>2的组合拳?(下)
阿里云智能数据库产品事业部高级产品专家德哥为大家带来如何将PostgreSQL与MySQL相结合,实现1+1>2的组合拳的介绍。 MySQL是最流行的开源数据库,PostgreSQL(PG)是最先进的开源数据库。两者结合,可以发挥1+1大于2的超级效果,解决应用原本无法解决的问题。 本议题将为您详细介绍如何将PG与MySQL结合使用,提升应用价值,以及5大业务场景和联合应用解读。
1333 0
如何将PostgreSQL与MySQL相结合,实现1+1>2的组合拳?(下)
|
SQL 并行计算 搜索推荐
如何将PostgreSQL与MySQL相结合,实现1+1>2的组合拳?(上)
阿里云智能数据库产品事业部高级产品专家德哥为大家带来如何将PostgreSQL与MySQL相结合,实现1+1&gt;2的组合拳的介绍。 MySQL是最流行的开源数据库,PostgreSQL(PG)是最先进的开源数据库。两者结合,可以发挥1+1大于2的超级效果,解决应用原本无法解决的问题。 本议题将为您详细介绍如何将PG与MySQL结合使用,提升应用价值,以及5大业务场景和联合应用解读。
1580 0
如何将PostgreSQL与MySQL相结合,实现1+1>2的组合拳?(上)