C#三层架构简单应用附具体代码.docx
- 文档编号:6488275
- 上传时间:2023-01-07
- 格式:DOCX
- 页数:19
- 大小:164.41KB
C#三层架构简单应用附具体代码.docx
《C#三层架构简单应用附具体代码.docx》由会员分享,可在线阅读,更多相关《C#三层架构简单应用附具体代码.docx(19页珍藏版)》请在冰豆网上搜索。
C#三层架构简单应用附具体代码
说明:
这是一个简单的三层架构的网站,实现信息的添加、显示以及一些页面的验证。
此解决方案包括三个类库(分别是Model,BLL,DAL)和一个网站项目。
Model封装实体类,DAL里包含操作数据库的相关函数,BLL只是简单调用DAL的方法(没有涉及业务规则),网站的App_Code文件夹有一个公共函数(主要实现页面弹出无白屏窗口,清空TextBox等功能),为了简单起见,只设计一个页面。
页面还用到VS自带的验证控件,还有简单的正则表达式。
数据库只调用一张表。
解决方案项目如下:
数据库db_student中的tb_Student表设计如下
一、下面先从实体类贴代码
Model里的StudentInfo.cs(比较简单)
namespaceModel
{
publicclassStudentInfo
{
//私有字段
privateintstu_ID;//学生的名字
privatestringstu_Name;//学生的名字
privatestringstu_Sex;//学生的性别
privatestringstu_Address;//学生的地址
privatestringstu_Tel;//学生的电话
privateintstu_Age;//学生的年龄
///
///属性:
学生的ID
///
publicintStu_ID
{
get{returnstu_ID;}
set{stu_ID=value;}
}
///
///属性:
学生的名字
///
publicstringStu_Name
{
get{returnstu_Name;}
set{stu_Name=value;}
}
///
///属性:
学生的性别
///
publicstringStu_Sex
{
get{returnstu_Sex;}
set{stu_Sex=value;}
}
///
///属性:
学生的地址
///
publicstringStu_Address
{
get{returnstu_Address;}
set{stu_Address=value;}
}
///
///属性:
学生的电话
///
publicstringStu_Tel
{
get{returnstu_Tel;}
set{stu_Tel=value;}
}
///
///属性:
学生的年龄
///
publicintStu_Age
{
get{returnstu_Age;}
set{stu_Age=value;}
}
}
}
二、DAL(StudentDAL.cs,引用Model)
usingSystem;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Data.SqlClient;
//引用实体层Model
usingModel;
namespaceDAL
{
publicclassStudentDAL
{
privateSqlConnectionConn=null;//数据源
privateSqlCommandCmd;//SQL命令,执行SQL语句或存储过程
privateStringstrConn;
///
///tb_StudentDAL的构造函数,读取web.config配置文件的连接字符串
///
publicStudentDAL()
{
strConn=ConfigurationManager.AppSettings["ConnStr"];
}
#region封装的一系列方法
///
///添加一条记录到数据库
///
///
///
publicboolInsertStudent(StudentInfostudent)
{
//定义一条插入的SQL语句
stringsql="insertintotb_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(ExecuteCmd(sql)>0)
{
returntrue;
}
else
{
returnfalse;
}
}
catch(System.Exceptionex)
{
throwex;
}
}
///
///根据学生ID删除一条记录
///
///
///
publicboolDeleteStudent(intstu_ID)
{
stringsql="deletefromtb_StudentwhereStu_ID="+stu_ID;
try
{
if(ExecuteCmd(sql)>0)
{
returntrue;
}
else
{
returnfalse;
}
}
catch(System.Exceptionex)
{
throwex;
}
}
///
///打开Conn连接
///
publicvoidOpen()
{
if(Conn==null)
Conn=newSqlConnection(strConn);
if(Conn.State.Equals(ConnectionState.Closed))
Conn.Open();
}
///
///关闭Conn连接并释放资源
///
publicvoidClose()
{
if(Conn.State.Equals(ConnectionState.Open))
{
Conn.Close();
Conn.Dispose();//释放占用的资源
}
else
Conn.Dispose();
}
///
///执行Cmd操作类
///
///
///
publicintExecuteCmd(stringsql)
{
try
{
Open();
Cmd=newSqlCommand(sql,Conn);
returnCmd.ExecuteNonQuery();
}
catch
{
return-1;
}
finally
{
Close();
}
}
#endregion
}
}
三、BLL(StudentBLL.cs,引用DAL和Model)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Configuration;
//引用DAL和Model
usingDAL;
usingModel;
namespaceBLL
{
publicclassStudentBLL
{
publicboolInsertStudent(StudentInfostudent)
{
StudentDALstudentDAL=newStudentDAL();
returnstudentDAL.InsertStudent(student);
}
publicboolDeleteStudent(intstu_ID)
{
StudentDALstudentDAL=newStudentDAL();
returnstudentDAL.DeleteStudent(stu_ID);
}
}
}
四、表现层(引用BLL,Model)
先看web.config文件,把连接字符串改成你电脑上相应的配置。
我的是Windows认证,实例为.\sqlexpress
Default.aspx页面
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:
//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
//www.w3.org/1999/xhtml">
.lblMessage
{
color:
red;
font-size:
150%;
}
添加一条记录到数据库的表里面
学生的ID:
TextBoxID="tbxID"runat="server">
TextBox>
RegularExpressionValidatorID="RegularExpressionValidator1" ControlToValidate="tbxID"runat="server"ValidationExpression="^([1-9][0-9]*)$" ErrorMessage="请输入大于1数字">
RegularExpressionValidator>
学生的姓名:
TextBoxID="tbxName"runat="server">
TextBox>
学生的性别:
TextBoxID="tbxSex"runat="server">
TextBox>
学生的地址:
TextBoxID="tbxAdress"runat="server">
TextBox>
学生的电话:
TextBoxID="tbxTel"runat="server">
TextBox>
学生的年龄:
TextBoxID="tbxAge"runat="server">
TextBox>
RegularExpressionValidatorID="RegularExpressionValidator2"ControlToValidate="tbxAge" ValidationExpression="^\+? [1-9][0-9]$"runat="server" ErrorMessage="年龄只允许10~99之间的数字">
RegularExpressionValidator>
ButtonID="btnSumit"runat="server"Text="提交"onclick="btnSumit_Click"/> LabelID="lblMessage"runat="server"Text=""Visible="false"EnableViewState="false" CssClass="lblMessage"> Label> GridViewID="grdStudent"runat="server"CellPadding="4" ForeColor="#333333"GridLines="None"DataKeyNames="Stu_ID"AutoGenerateColumns="false"> BoundFieldDataField="Stu_ID"HeaderText="学生ID"/> BoundFieldDataField="Stu_Name"HeaderText="学生姓名"/> BoundFieldDataField="Stu_Sex"HeaderText="性别"/> BoundFieldDataField="Stu_Address"HeaderText="地址"/> BoundFieldDataField="Stu_Tel"HeaderText="联系电话"/> BoundFieldDataField="Stu_Age"HeaderText="年龄"/> GridView>