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

上传人:b****5 文档编号:6488275 上传时间:2023-01-07 格式:DOCX 页数:19 大小:164.41KB
下载 相关 举报
C#三层架构简单应用附具体代码.docx_第1页
第1页 / 共19页
C#三层架构简单应用附具体代码.docx_第2页
第2页 / 共19页
C#三层架构简单应用附具体代码.docx_第3页
第3页 / 共19页
C#三层架构简单应用附具体代码.docx_第4页
第4页 / 共19页
C#三层架构简单应用附具体代码.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

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

《C#三层架构简单应用附具体代码.docx》由会员分享,可在线阅读,更多相关《C#三层架构简单应用附具体代码.docx(19页珍藏版)》请在冰豆网上搜索。

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

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封装的一系列方法

///

///添加一条记录到数据库

///

///student实体

///TrueorFalse

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操作类

///

///sql语句

///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>

 

Default.aspx.cs后台代码

usingSystem;

usingSystem.Configuration;

usingSystem.Data;

usingSystem.Data.SqlClient;

usingSystem.Web.UI.WebControls;

usingBLL;

usingModel;

publicpartialclass_Default:

System.Web.UI.Page

{

StudentInfostudent=newStudentInfo();

StudentBLLstudentBLL=newStudentBLL();

Commoncommon=newCommon();

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

grdStudent.DataSource=GetData();

grdStudent.DataBind();

}

}

//返回数据集函数

protectedDataSetGetData()

{

stringsql="select*fromtb_Student";

SqlConnectionconn=newSqlConnection(ConfigurationManager.AppSettings["connStr"]);

conn.Open();

SqlDataAdapterda=newSqlDataAdapter(sql,conn);

DataSetds=newDataSet();

da.Fill(ds);

conn.Close();

returnds;

}

//提交事件

protectedvoidbtnSumit_Click(objectsender,EventArgse)

{

if(!

common.ExitString(tbxID,tbxName,tbxSex,tbxTel,tbxAge,tbxAdress))

{

Common.ShowMessages(this,"请填写所有的输入区域!

");

return;

}

student.Stu_ID=Convert.ToInt32(tbxID.Text.Trim());

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

{

BooleanOK=studentBLL.InsertStudent(student);

if(OK)

{

Common.ShowMessages(this,"插入成功!

","Default.aspx");

//清空TextBox

common.ClearInput(tbxID,tbxName,tbxSex,tbxAdress,tbxAge,tbxTel);

}

else

{

Common.ShowMessages(this,"插入的学生ID不能重复!

");

}

}

catch(SqlExceptionex)

{

throwex;

}

}

}

 

App_Code文件夹里的公共方法Common.cs代码如下:

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

///

///Common的摘要说明

///

publicclassCommon

{

publicCommon()

{

//

//TODO:

在此处添加构造函数逻辑

//

}

///

///Common方法:

判断TextBox等控件中值是存在的

///

///

///

publicboolExitString(paramsControl[]tempStr)

{

boolExitbool=true;

for(inti=0;i

{

try

{

if(tempStr[i]isTextBox)//如果是TextBox控件

{

if(string.IsNullOrEmpty(((TextBox)(tempStr[i])).Text.Trim()))

{

Exitbool=false;

break;

}

}

elseif(tempStr[i]isDropDownList)//如果是DropDownList控件

{

if(string.IsNullOrEmpty(((DropDownList)(tempStr[i])).SelectedValue))

{

Exitbool=false;

break;

}

}

elseif(tempStr[i]isFileUpload)//如果是FileUpload控件

{

if(string.IsNullOrEmpty(((FileUpload)tempStr[i]).PostedFile.FileName))

{

Exitbool=false;

break;

}

}

}

catch(System.Exceptionex)

{

Exitbool=false;

}

}

returnExitbool;

}

///

///说明:

清空输入区域

///功能:

将所有TextBox的输入信息清空

///

///

publicvoidClearInput(paramsControl[]control)

{

for(inti=0;i

{

if(control[i]isTextBox

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1