用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与三层架构
虽然接触了两者有一段时间了,但是有时还是会混淆概念,在此处不打算说明二者的区别,因为二者都是架构模式,并且也有一定的共存度,在实际开发中,严格区分意义不大.基于最近涉及到这部分知识就在复习下,编程过程 ...
随机推荐
- Iphone5s 通话质量差 问题解决
从某段时间开始,我的手机出现通话质差的情况.而且与信号电平有密切关系,只要覆盖稍差的地方,通话就劣化. 具体症状是我时常听不清对方说话,但对方能够清晰听到我说话. 因为本人也是从事无线网络优化软件产品 ...
- AlarmManager使用注意事项
在使用AlarmManager实现闹钟需要注意的是,intent和pendingintend的context如果是activity,那么当activity回收之后,context对象则不能被Alarm ...
- android开发类似coverflow效果的3d旋转
源码下载地址:http://download.csdn.net/detail/feijian_/8888219
- Jquery结合Ztree生成树
尊重作者,附原文链接:http://my.oschina.net/u/2472104/blog/529055 Ztree的api http://www.ztree.me/v3/api.php Ztre ...
- STL容器的适用情况
转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...
- python-面向对象(指数对象举例)
class Index(object): def __init__(self,index_name,index_code,closePrice_yesterday,closePrice_today): ...
- 浅谈GitHub
Git 是一个面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub. Gith是一个基于 git 的社会化代码分享社区,所谓 social coding.你 ...
- 01.安装Memcached
1.安装Memcached 1.下载Memcached及其依赖 下载memcached-1.4.24.tar.gz和libevent-2.0.22-stable.tar.gz文件并解压如下: [liz ...
- 【python】编码规范(转载)
转自:http://www.cnblogs.com/itech/archive/2012/01/06/2314454.html 1 编码 >>所有的 Python 脚本文件都应在文件头标上 ...
- JSP页面批量选择&全选操作&选择回显
效果如下: js验证部分: 页面body部分: 附:控制器Controller中验证批量选择条件回显: