用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与三层架构
虽然接触了两者有一段时间了,但是有时还是会混淆概念,在此处不打算说明二者的区别,因为二者都是架构模式,并且也有一定的共存度,在实际开发中,严格区分意义不大.基于最近涉及到这部分知识就在复习下,编程过程 ...
随机推荐
- 设置VS2010中自带的ActiveX控件测试容器TstCon
ActiveX控件:可以看做一个极小的服务器应用程序,他不能单独运行,需要嵌入到某个程序中才可以运行,我们可以自己写一个程序来测试自己写的程序(具体方法在下一篇文章阐述),第二种方法是利用VS(本人编 ...
- SQL Server 2008 的安装
SQL Server 2008简体中文企业版下载(SQL2008) SQL Server 2008分为SQL Server 2008企业版.标准版.工作组版.Web版.开发者版.Express版.Co ...
- oracle关于分区相关操作
[sql] view plaincopy 1.查询当前用户下有哪些是分区表: SELECT * FROM USER_PART_TABLES; 2.查询当前用户下有哪些分区索引: SELECT * FR ...
- 如何用myeclispe远程调试tomcat
如何用myeclispe远程调试tomcat 在工作开发中,通常用本机进行代码编写,然后将编好的工程部署到测试服务器进行测试.往往测试服务器并不是自己的本机,因此对调试带来众多不便.今天学习可以用my ...
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
- 链路层三种类型的MAC地址
若需要转载,请注明出处. 我们知道,链路层都是以MAC地址来进行通信双方的地址标识的,如下图:在应用中根据接收方的多寡来进行划分,可分为以下三种: 单播(Unicast) 多播(Multicast) ...
- Mac上安装 mySql
今天在mac系统上安装了 mySql 和大家分享下 安装的过程.. 首先 第一步 需要在oracle的网站下载 mysql 的mac 版本. 下载地址如下: http://www.mysql.com/ ...
- Winform DataGridView扩展
public class ObjectPropertyCompare<T> : IComparer<T> { private readonly PropertyDescript ...
- Brush Mode --- Nyoj 737 分类: Brush Mode 2014-03-25 08:10 202人阅读 评论(0) 收藏
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆 ...
- ajax跨域请求,页面和java服务端的写法
方法一(jsonp): 页面ajax请求的写法: $.ajax({ type : "get", async : false, cache : false, url : " ...