最适合入门学习的三层架构例子(实现登录)
2011-09-20 09:33
561 查看
新建各层——添加各层引用——编写各层.cs代码。
http://bbs.51aspx.com/showtopic-3620.html
1.打开VS2008后,文件-->新建-->项目-->其他项目类型-->VisualStudio解决方案-->空白解决方案就起名为:MvcTest吧
2.建立如图
2.jpg(10.90K)
2008-6-913:27:47
的项目,并在WEB-->App_Data建一个数据文件DabaBase.mdf里面建表:qzzm_user表内:字段Name,类型:nvarchar(50)
3.在WEB中引用BLL,Model层新建Post.aspx
CODE:
<%@PageLanguage=&quot;C#&quot;AutoEventWireup=&quot;true&quot;CodeFile=&quot;Post.aspx.cs&quot;Inherits=&quot;Post&quot;%>
<!DOCTYPEhtmlPUBLIC&quot;-//W3C//DTDXHTML1.0Transitional//EN&quot;&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>
<htmlxmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
<headrunat=&quot;server&quot;>
<title>无标题页</title>
</head>
<body>
<formid=&quot;form1&quot;runat=&quot;server&quot;>
<div>
<asp:TextBoxID=&quot;tb_name&quot;runat=&quot;server&quot;></asp:TextBox>
<asp:ButtonID=&quot;btn_post&quot;runat=&quot;server&quot;onclick=&quot;btn_post_Click&quot;Text=&quot;提交&quot;/>
</div>
</form>
</body>
</html>
Post.aspx.cs先搁下等写好类库再写
4.在Model实体类中新建一个user.cs的类
CODE:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceModel
{
publicclassuser
{
publicuser(){}
privatestring_Name;
publicstringName
{
set{_Name=value;}
get{return_Name;}
}
}
}
5.在DAL新建userdb.cs,并引用Model层
CODE:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
namespaceDAL
{
publicclassuserdb
{
publicbooladduser(Model.usermodel)
{
SqlConnectioncon=newSqlConnection(ConfigurationManager.ConnectionStrings[&quot;sqlconn&quot;].ConnectionString);
con.Open();
using(SqlCommandcmd=newSqlCommand(&quot;INSERTINTOqzzm_user(Name)VALUES(@Name)&quot;,con))
{
cmd.Parameters.AddWithValue(&quot;@Name&quot;,model.Name);
if(cmd.ExecuteNonQuery()>0)
returntrue;
elsereturnfalse;
}
}
}
}
6.在BLL中新建userbll.cs并引用DAL,Model层
CODE:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceBLL
{
publicclassuserbll
{
DAL.userdbdb=newDAL.userdb();
publicbooladduser(Model.usermodel)
{
returndb.adduser(model);
}
}
}
7.可以开始写Post.aspx.cs了
CODE:
usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
publicpartialclassPost:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidbtn_post_Click(objectsender,EventArgse)
{
Model.userus=newModel.user();
us.Name=tb_name.Text;
BLL.userbllub=newBLL.userbll();
ub.adduser(us);
}
}
8.补充之前少写的Web.config的数据链接字符串
CODE:
<connectionStrings>
<addname=&quot;sqlconn&quot;connectionString=&quot;DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;IntegratedSecurity=True;UserInstance=True&quot;providerName=&quot;System.Data.SqlClient&quot;/>
</connectionStrings>
最后,说下各层的引用关系:
1)WEB引用DAL,Model
2)BLL引用DAL,Model
3)DAL引用Model
4)Model无引用
拓展说明:本小例子只是从三层思路考虑,对业务逻辑层没有写特别的代码,只用来引用DAL,大家可以根据51aspx.com中的一些三层留言本来进行学习,便可写出更好的三层代码..到此结束吧,最后附上本人的源代码方便大家浏览.(P.S源代友中有个Utility类库是用来显示信息的,可能类库命名错了
,同时希望51aspx老大给文章加个精~)
源码下载地址:http://www.51aspx.com/CV/MVCTest
asp.net中如何查看表示层的引用关系?
是项目依赖选项,且只能用于非web层才能显示在右键菜单中.
1.打开VS2008后,文件-->新建-->项目-->其他项目类型-->VisualStudio解决方案-->空白解决方案就起名为:MvcTest吧
2.建立如图
2008-6-913:27:47
的项目,并在WEB-->App_Data建一个数据文件DabaBase.mdf里面建表:qzzm_user表内:字段Name,类型:nvarchar(50)
3.在WEB中引用BLL,Model层新建Post.aspx
CODE:
<%@PageLanguage=&quot;C#&quot;AutoEventWireup=&quot;true&quot;CodeFile=&quot;Post.aspx.cs&quot;Inherits=&quot;Post&quot;%>
<!DOCTYPEhtmlPUBLIC&quot;-//W3C//DTDXHTML1.0Transitional//EN&quot;&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>
<htmlxmlns=&quot;http://www.w3.org/1999/xhtml&quot;>
<headrunat=&quot;server&quot;>
<title>无标题页</title>
</head>
<body>
<formid=&quot;form1&quot;runat=&quot;server&quot;>
<div>
<asp:TextBoxID=&quot;tb_name&quot;runat=&quot;server&quot;></asp:TextBox>
<asp:ButtonID=&quot;btn_post&quot;runat=&quot;server&quot;onclick=&quot;btn_post_Click&quot;Text=&quot;提交&quot;/>
</div>
</form>
</body>
</html>
Post.aspx.cs先搁下等写好类库再写
4.在Model实体类中新建一个user.cs的类
CODE:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceModel
{
publicclassuser
{
publicuser(){}
privatestring_Name;
publicstringName
{
set{_Name=value;}
get{return_Name;}
}
}
}
5.在DAL新建userdb.cs,并引用Model层
CODE:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
namespaceDAL
{
publicclassuserdb
{
publicbooladduser(Model.usermodel)
{
SqlConnectioncon=newSqlConnection(ConfigurationManager.ConnectionStrings[&quot;sqlconn&quot;].ConnectionString);
con.Open();
using(SqlCommandcmd=newSqlCommand(&quot;INSERTINTOqzzm_user(Name)VALUES(@Name)&quot;,con))
{
cmd.Parameters.AddWithValue(&quot;@Name&quot;,model.Name);
if(cmd.ExecuteNonQuery()>0)
returntrue;
elsereturnfalse;
}
}
}
}
6.在BLL中新建userbll.cs并引用DAL,Model层
CODE:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceBLL
{
publicclassuserbll
{
DAL.userdbdb=newDAL.userdb();
publicbooladduser(Model.usermodel)
{
returndb.adduser(model);
}
}
}
7.可以开始写Post.aspx.cs了
CODE:
usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
publicpartialclassPost:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidbtn_post_Click(objectsender,EventArgse)
{
Model.userus=newModel.user();
us.Name=tb_name.Text;
BLL.userbllub=newBLL.userbll();
ub.adduser(us);
}
}
8.补充之前少写的Web.config的数据链接字符串
CODE:
<connectionStrings>
<addname=&quot;sqlconn&quot;connectionString=&quot;DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;IntegratedSecurity=True;UserInstance=True&quot;providerName=&quot;System.Data.SqlClient&quot;/>
</connectionStrings>
最后,说下各层的引用关系:
1)WEB引用DAL,Model
2)BLL引用DAL,Model
3)DAL引用Model
4)Model无引用
拓展说明:本小例子只是从三层思路考虑,对业务逻辑层没有写特别的代码,只用来引用DAL,大家可以根据51aspx.com中的一些三层留言本来进行学习,便可写出更好的三层代码..到此结束吧,最后附上本人的源代码方便大家浏览.(P.S源代友中有个Utility类库是用来显示信息的,可能类库命名错了
,同时希望51aspx老大给文章加个精~)
源码下载地址:
相关文章推荐
- 基于MVC和三层架构,用jsp-Servlet-JavaBean实现登录和注册
- 很不多的ICE架构入门学习例子-ping程序
- VB.net+三层架构 实现登录功能
- C# 三层架构之系统的登录验证与添加数据的实现
- 三层架构在登录窗体中的实现(一)
- 最简单易理解的Flex PureMVC入门例子:实现登录
- JAVA WEB从入门到精通day14 JAVA三层架构、登录注册实战演练
- 数据库基础入门(四)— — 产品功能实现之数据库架构与用户登录注册
- 用三层架构写实现用户登录(ASP.NET)
- 三层架构在登录窗口中再实现(二)
- 求:用接口实现三层架构的例子
- 【三层学习】——VB.NET实现登录
- ASP.NET 三层架构实现用户登录
- 数据库基础入门(四)— — 产品功能实现之数据库架构与用户登录注册
- winform学习日志(十九)----------真正三层架构之登录
- 三层架构实战篇——抽象工厂+反射实现验证用户登录!
- 最简单,最适合入门学习的三层架构例子
- 很不多的ICE架构入门学习例子-ping程序
- Winform 三层架构例子(3)--利用资源文件实现多国语言(含源代码)
- asp.net实现三层架构的例子