ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:164.41KB ,
资源ID:6488275      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6488275.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#三层架构简单应用附具体代码.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C#三层架构简单应用附具体代码.docx

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