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. python3.4 安装ipython notebook

    1.安装python3.4 2.安装pyreadline 3.安装ipython-1.2.1.zip 4.安装pyzmq-14.7.0-cp34-none-win32.whl,ZIP包有问题,下载wh ...

  2. 多线程 -- NSOperation

    NSOperation 此类不能直接使用 NSInvocationOperation NSBlockOperation 定义一个类继承与它 NSInvocationOperation 可以使用star ...

  3. C#日志编写

    在一个完整的信息系统里面,日志系统是一个非常重要的功能组成部分.它可以记录下系统所产生的所有行为,并按照某种规范表达出来.我们可以使用日志系统所记录的信息为系统进行排错,优化系统的性能,或者根据这些信 ...

  4. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  5. 【bzoj1085】[SCOI2005]骑士精神

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1757  Solved: 961[Submit][Statu ...

  6. JS数组2(冒泡排列、数组里面查找数据)

    数组 一.冒泡排列 对数组attr = [1,8,6,4,5,3,7,2,9]进行由大到小排列,用冒泡排列的方法排列时,会对数组进行比较互换.如果前一个数字较大,这2个元素排列方式不变,如果后一个元素 ...

  7. 实现WMSservice的时候,出现边缘的点或icon被切断的情况

    可以通过为实际查询的boundary加一个buffer,使查询的范围比指定的大一点点,这样就会使tile之间在查询的时候有一定的重叠. 如:Geometry queryBoundary = JTS.t ...

  8. maven mirror repository

    简单点来说,repository就是个仓库.maven里有两种仓库,本地仓库和远程仓库.远程仓库相当于公共的仓库,大家都能看到.本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用.当你向仓库请 ...

  9. Delphi中关于资源释放(Free,Relealse,FreeAndNil)

    根据日常编程经验,得出一些Delphi中关于资源释放的体会. 假如有对象Obj为TObject类型: 1) Obj.Free直接释放资源后,调用OnDestroy事件,但是没有将Obj指针值置为Nil ...

  10. POJ 2480 Longge's problem (积性函数,欧拉函数)

    题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...