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实现三层(复习)的更多相关文章

  1. asp.net -mvc框架复习(10)-基于三层架构与MVC搭建项目框架

    一.三种模式比较 1.MVC框架(适合大型项目) (1).V视图 (网页部分) (2).M模型 (业务逻辑+数据访问+实体类) (3).C控制器 (介于M和V之间,起到引导作用) 2.三层架构 (1) ...

  2. ssh三大框架,三层架构 整合测试!完整分页代码,JdbcTemplate等测试,存储过程调用,留着以后复习吧

    下载地址:http://download.csdn.net/detail/liangrui1988/5760453

  3. asp.net -mvc框架复习(11)-基于三层架构与MVC实现完整的用户登录

    一.先从M部分写起(Modles\DAL\BLL) 1.Modles 实体类:上次实体类已经搞定. 2.DAL 数据访问类类 (1)通用数据数据访问类: A:  先编写数据连接字符串,写到网站根目录W ...

  4. 《Linux内核分析》第四周 扒开系统调用的“三层皮”

    [刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK FOUR( ...

  5. vb.net三层实现登录例子

    看三层已经很长时间了,中间有经过了期末考试.回家等等琐事,寒假开学的我已经回想不起什么事三层了,经过了三四天的重新复习,再加上查看各期师哥师姐的博客,终于,自己完成了C#视频中的登录小例子,下面就和大 ...

  6. LINUX内核分析第四周学习总结——扒开应用系统的三层皮(上)【转】

    转自:http://www.cnblogs.com/lalacindy/p/5276874.html 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://moo ...

  7. 数往知来 三层架构 <十四>

    三层架构_1 一.三层 就是把程序的各个部分都分离,尽量的底耦合,做到分工明确.责任明确 第一层:Dal   数据访问层 第二层 :Bll  业务逻辑判断层 第三层: UI   界面显示层 比如说数据 ...

  8. Angular JS从入门基础 mvc三层架构 常用指令

    Angular JS从入门基础  mvc模型 常用指令 ★ 最近一直在复习AngularJS,它是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核心 ...

  9. .NET MVC与三层架构

    虽然接触了两者有一段时间了,但是有时还是会混淆概念,在此处不打算说明二者的区别,因为二者都是架构模式,并且也有一定的共存度,在实际开发中,严格区分意义不大.基于最近涉及到这部分知识就在复习下,编程过程 ...

随机推荐

  1. Mysql 导出数据库和指定表中的数据

    参考地址:http://jingyan.baidu.com/article/b7001fe14240ab0e7282dde9.html [root@youo zw]# mysqldump -u roo ...

  2. Java 7 中 NIO.2 的使用——文件递归操作

    众所周知,递归编程是一项有争议的技术,因为它需要大量的内存,但是它能简化一些编程任务.基本上,一个递归操作都是程序调用自己传递参数修改的值或者参数传递到当前的程序循环中.递归编程通常用来计算阶乘斐波那 ...

  3. 【BZOJ】【1150】【CTSC2007】数据备份Backup

    堆/贪心 一共N-1个元素……用堆维护最大值,取了第x个元素以后,插入v[x-1]+v[x+1]-v[x]这个元素,如果再取这个新元素就表示不取x,而取x-1和x+1……大概就是这种“带反悔”的思路吧 ...

  4. Codeforce 438D-The Child and Sequence 分类: Brush Mode 2014-10-06 20:20 102人阅读 评论(0) 收藏

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  5. ALAssetsLibrary

    ALAssetsLibrary详解   ALAssetsLibrary类是代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能. _library = [[ALAssetsLibr ...

  6. 聊一聊js中的null、undefined与NaN

    零.寒暄 翻翻自己的博客,上一篇竟然是六月26号的,说好的更新呢?回顾刚刚过去的这个七月,整天都是公司的入职培训加上自己的小论文,每天奋战到凌晨1点多,这是要挂的节奏啊!但是不论怎么说,自己的时间管理 ...

  7. 编译libcore-amr静态库

    在此链接下 https://github.com/feuvan/opencore-amr-iOS 下载它的源码到本地, 然后cd到此目录下,在终端输入命令./build_ios_xcode6.sh,便 ...

  8. Codeforces Round #364 (Div. 2)->A. Cards

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. HTML5中表单验证的8种方法(转)

    在深人探讨表单验证之前,让我们先思考一下表单验证的真实含义.就其核心而言,表单验证是一套系统,它为终端用户检测无效的控件数据并标记这些错误.换言之,表单验证就是在表单提交服务器前对其进行一系列的检查并 ...

  10. Linux常用命令大全(转载)

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...