1、C#三层架构简单应用附具体代码说明:这是一个简单的三层架构的网站,实现信息的添加、显示以及一些页面的验证。此解决方案包括三个类库(分别是Model,BLL,DAL)和一个网站项目。Model封装实体类,DAL里包含操作数据库的相关函数,BLL只是简单调用DAL的方法(没有涉及业务规则),网站的App_Code文件夹有一个公共函数(主要实现页面弹出无白屏窗口,清空TextBox等功能),为了简单起见,只设计一个页面。页面还用到VS自带的验证控件,还有简单的正则表达式。数据库只调用一张表。解决方案项目如下:数据库db_student中的tb_Student表设计如下一、下面先从实体类贴代码Mode
2、l里的StudentInfo.cs(比较简单)namespace Model public class StudentInfo /私有字段 private int stu_ID; /学生的名字 private string stu_Name; /学生的名字 private string stu_Sex; /学生的性别 private string stu_Address; /学生的地址 private string stu_Tel; /学生的电话 private int stu_Age; /学生的年龄 / / 属性:学生的ID / public int Stu_ID get return st
3、u_ID; set stu_ID = value; / / 属性:学生的名字 / public string Stu_Name get return stu_Name; set stu_Name = value; / / 属性:学生的性别 / public string Stu_Sex get return stu_Sex; set stu_Sex = value; / / 属性:学生的地址 / public string Stu_Address get return stu_Address; set stu_Address = value; / / 属性:学生的电话 / public str
4、ing Stu_Tel get return stu_Tel; set stu_Tel = value; / / 属性:学生的年龄 / public int Stu_Age get return stu_Age; set stu_Age = value; 二、DAL(StudentDAL.cs,引用Model)using System;using System.Configuration;using System.Data;using System.Data.SqlClient;/引用实体层Modelusing Model;namespace DAL public class StudentD
5、AL private SqlConnection Conn = null; /数据源 private SqlCommand Cmd; /SQL命令,执行SQL语句或存储过程 private String strConn; / / tb_StudentDAL的构造函数,读取web.config配置文件的连接字符串 / public StudentDAL() strConn = ConfigurationManager.AppSettingsConnStr; #region 封装的一系列方法 / / 添加一条记录到数据库 / / student实体 / True or False public b
6、ool InsertStudent(StudentInfo student) /定义一条插入的SQL语句 string sql = insert into tb_Student(Stu_ID,Stu_Name,Stu_Sex,Stu_Address,Stu_Tel,Stu_Age)values( + student.Stu_ID + , + student.Stu_Name + , + student.Stu_Sex + , + student.Stu_Address + , + student.Stu_Tel + , + student.Stu_Age + ); try if (Execut
7、eCmd(sql) 0) return true; else return false; catch (System.Exception ex) throw ex; / / 根据学生ID删除一条记录 / / / public bool DeleteStudent(int stu_ID) string sql = delete from tb_Student where Stu_ID= + stu_ID; try if (ExecuteCmd(sql) 0) return true; else return false; catch (System.Exception ex) throw ex;
8、 / / 打开Conn连接 / public void Open() if (Conn = null) Conn = new SqlConnection(strConn); if (Conn.State.Equals(ConnectionState.Closed) Conn.Open(); / / 关闭Conn连接并释放资源 / public void Close() if (Conn.State.Equals(ConnectionState.Open) Conn.Close(); Conn.Dispose();/释放占用的资源 else Conn.Dispose(); / / 执行Cmd操作
9、类 / / sql语句 / Cmd public int ExecuteCmd(string sql) try Open(); Cmd = new SqlCommand(sql, Conn); return Cmd.ExecuteNonQuery(); catch return -1; finally Close(); #endregion 三、BLL(StudentBLL.cs,引用DAL和Model)using System;using System.Collections.Generic;using System.Text;using System.Data;using System.C
10、onfiguration;/引用DAL和Modelusing DAL;using Model;namespace BLL public class StudentBLL public bool InsertStudent(StudentInfo student) StudentDAL studentDAL = new StudentDAL(); return studentDAL.InsertStudent(student); public bool DeleteStudent(int stu_ID) StudentDAL studentDAL = new StudentDAL(); retu
11、rn studentDAL.DeleteStudent(stu_ID); 四、表现层(引用BLL,Model)先看web.config文件,把连接字符串改成你电脑上相应的配置。我的是Windows认证,实例为.sqlexpress Default.aspx页面 无标题页 .lblMessage color:red; font-size:150%; 添加一条记录到数据库的表里面 学生的ID: 学生的姓名: 学生的性别: 学生的地址: 学生的电话: 学生的年龄: Default.aspx.cs后台代码using System;using System.Configuration;using Sys
12、tem.Data;using System.Data.SqlClient;using System.Web.UI.WebControls;using BLL;using Model;public partial class _Default : System.Web.UI.Page StudentInfo student = new StudentInfo(); StudentBLL studentBLL = new StudentBLL(); Common common = new Common(); protected void Page_Load(object sender, Event
13、Args e) if (!IsPostBack) grdStudent.DataSource= GetData(); grdStudent.DataBind(); /返回数据集函数 protected DataSet GetData() string sql = select * from tb_Student; SqlConnection conn=new SqlConnection(ConfigurationManager.AppSettingsconnStr); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sql, conn);
14、 DataSet ds = new DataSet(); da.Fill(ds); conn.Close(); return ds; /提交事件 protected void btnSumit_Click(object sender, EventArgs e) if (!common.ExitString(tbxID, tbxName, tbxSex, tbxTel, tbxAge, tbxAdress) Common.ShowMessages(this, 请填写所有的输入区域!); return; student.Stu_ID = Convert.ToInt32(tbxID.Text.Tri
15、m(); student.Stu_Name = tbxName.Text; student.Stu_Sex = tbxSex.Text; student.Stu_Address = tbxAdress.Text; student.Stu_Tel = tbxTel.Text; student.Stu_Age = Convert.ToInt32(tbxAge.Text.Trim(); /调用BLL层的插入方法 try Boolean OK = studentBLL.InsertStudent(student); if (OK) Common.ShowMessages(this, 插入成功!, De
16、fault.aspx); /清空TextBox common.ClearInput(tbxID, tbxName, tbxSex, tbxAdress, tbxAge, tbxTel); else Common.ShowMessages(this, 插入的学生ID不能重复!); catch (SqlException ex) throw ex; App_Code文件夹里的公共方法Common.cs代码如下:using System.Web.UI;using System.Web.UI.WebControls;/ /Common 的摘要说明/ public class Common public
17、 Common() / /TODO: 在此处添加构造函数逻辑 / / / Common方法:判断TextBox等控件中值是存在的 / / / public bool ExitString(params Control tempStr) bool Exitbool = true; for (int i = 0; i tempStr.Length; i+) try if (tempStri is TextBox) /如果是TextBox控件 if (string.IsNullOrEmpty(TextBox)(tempStri).Text.Trim() Exitbool = false; break
18、; else if (tempStri is DropDownList) /如果是DropDownList控件 if (string.IsNullOrEmpty(DropDownList)(tempStri).SelectedValue) Exitbool = false; break; else if (tempStri is FileUpload) /如果是FileUpload控件 if (string.IsNullOrEmpty(FileUpload)tempStri).PostedFile.FileName) Exitbool = false; break; catch (System.Exception ex) Exitbool = false; return Exitbool; / / 说明:清空输入区域 / 功能:将所有TextBox的输入信息清空 / / public void ClearInput(params Control control) for (int i = 0; i control.Length; i+) if (controli is TextBox
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1