用PostGreSQL实现三层(复习)
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实现三层(复习)的更多相关文章
- asp.net -mvc框架复习(10)-基于三层架构与MVC搭建项目框架
一.三种模式比较 1.MVC框架(适合大型项目) (1).V视图 (网页部分) (2).M模型 (业务逻辑+数据访问+实体类) (3).C控制器 (介于M和V之间,起到引导作用) 2.三层架构 (1) ...
- ssh三大框架,三层架构 整合测试!完整分页代码,JdbcTemplate等测试,存储过程调用,留着以后复习吧
下载地址:http://download.csdn.net/detail/liangrui1988/5760453
- asp.net -mvc框架复习(11)-基于三层架构与MVC实现完整的用户登录
一.先从M部分写起(Modles\DAL\BLL) 1.Modles 实体类:上次实体类已经搞定. 2.DAL 数据访问类类 (1)通用数据数据访问类: A: 先编写数据连接字符串,写到网站根目录W ...
- 《Linux内核分析》第四周 扒开系统调用的“三层皮”
[刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK FOUR( ...
- vb.net三层实现登录例子
看三层已经很长时间了,中间有经过了期末考试.回家等等琐事,寒假开学的我已经回想不起什么事三层了,经过了三四天的重新复习,再加上查看各期师哥师姐的博客,终于,自己完成了C#视频中的登录小例子,下面就和大 ...
- LINUX内核分析第四周学习总结——扒开应用系统的三层皮(上)【转】
转自:http://www.cnblogs.com/lalacindy/p/5276874.html 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://moo ...
- 数往知来 三层架构 <十四>
三层架构_1 一.三层 就是把程序的各个部分都分离,尽量的底耦合,做到分工明确.责任明确 第一层:Dal 数据访问层 第二层 :Bll 业务逻辑判断层 第三层: UI 界面显示层 比如说数据 ...
- Angular JS从入门基础 mvc三层架构 常用指令
Angular JS从入门基础 mvc模型 常用指令 ★ 最近一直在复习AngularJS,它是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核心 ...
- .NET MVC与三层架构
虽然接触了两者有一段时间了,但是有时还是会混淆概念,在此处不打算说明二者的区别,因为二者都是架构模式,并且也有一定的共存度,在实际开发中,严格区分意义不大.基于最近涉及到这部分知识就在复习下,编程过程 ...
随机推荐
- MATLAB GUI程序设计中使文本框接收多行输入的方法
对于文本框来说 Max属性于Min属性数值之差小于等于1时,仅接收单行输入 大于1时,接受多行输入 对于多行情况,set/get到的String应为cell 本系列文章允许转载,转载请保留全文! [说 ...
- 使用Log4j进行日志操作
使用Log4j进行日志操作 一.Log4j简介 (1)概述 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接字服 ...
- Notes of the scrum meeting(12.5)
meeting time:18:00~18:30p.m.,December 5th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- 解释型语言和编译型语言如何交互?以lua和c为例
转自http://my.oschina.net/mayqlzu/blog/113528 问题: 最近lua很火,因为<愤怒的小鸟>使用了lua,ios上有lua解释器?它是怎么嵌入大ios ...
- float和CGFloat混用的风险
一般意义上的混用是没有问题的, 比如 float x=5.0; (void)printNumber:(CGFloat)number; 当调用printNumber:x的时候是没有问题的 但是如果使用f ...
- 802.11 wireless 三
802.11 wireless 3watts,milliwatts,and Decibels瓦特(功率单位)的定义是1焦耳/秒微波炉1000瓦特,手机100-200毫瓦 decibels(分贝:比较能 ...
- bzoj 2038 莫队算法
莫队算法,具体的可以看10年莫涛的论文. 大题思路就是假设对于区间l,r我们有了一个答案,那么对于区间l,r+1,我们 可以暴力的转移一个答案,那么对于区间l1,r1和区间l2,r2,需要暴力处理 的 ...
- 【转载】让c++ 函数返回一个数组
在c++中是不允许数组作为函数的返回值的 int [] someFunction( ); //ILLEGAL 要想实现函数返回一个数组,那返回对应数组里面类型的指针 you must return a ...
- 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结
一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...
- HackPorts – Mac OS X 渗透测试框架与工具
HackPorts是一个OS X 下的一个渗透框架. HackPorts是一个“超级工程”,充分利用现有的代码移植工作,安全专业人员现在可以使用数以百计的渗透工具在Mac系统中,而不需要虚拟机. 工具 ...