创建一个asp.net三层架构
2011-08-22 14:21
302 查看
摘自:http://apps.hi.baidu.com/share/detail/19842513
创建一个asp.net三层架构
一个简单的三层结构demo:
首先我们来创建一个数据库ThreeLayer
可以把下面的:sql语句复制到查询分析器直接执行
use master
go
if exists (select * from sysdatabases where name=ThreeLayer)
drop database ThreeLayer
go
create database ThreeLayer
on
(
name='ThreeLayer_data',
filename='D:\SQLSERVER2000\MSSQL\Data\ThreeLayer_data.mdf',--我SQL装在D盘,你们可以换。
size=1mb,
filegrowth=10%
)
log on
(
name='ThreeLayer_log',
filename='D:\SQLSERVER2000\MSSQL\Data\ThreeLayer_log.ldf',
size=1mb,
filegrowth=10%
)
接着创建一张表Admin
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Admin]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Admin]
GO
CREATE TABLE [dbo].[Admin] (
[AdminId] [int] IDENTITY (1, 1) NOT NULL ,
[AdminUID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[AdminPWD] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
现在已经完成了数据库以及表的创建,接下来我们该创建三层架构的asp.net网站了。
一、打开vs.net 新建一个网站。命名为WebUIView,当然这个名字可以自己命名。
很明显,这个就是UI层,没错,这是给用户看的UI层。
二、我们先一个新项目命名为Model,这个实体层。
三、同样的道理我们添加一个新项目命名为DAL,这个数据访问层。
四、我们再添加一个新项目命名为BLL,这个数业务逻辑层。
接着,我们来添加引用,首先UI层引用BLL和Model,DAL引用Model,BLL引用DAL和Model
这样就完成了三层的初步架构。
当然实现三层的顺序是 Model--》DAL--》BLL--》UI
现在我把各个层的代码贴出来,首先是Model的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/****************实体层**********************/
namespace Model
{
public class LoginModel
{
#region 私有字段
private int _AdminId;
private string _AdminUID;
private string _AdminPWD;
#endregion
#region 属性
public int AdminId
{
set { this._AdminId=value; }
get { return _AdminId; }
}
public string AdminUID
{
set
{
this._AdminUID = value;
}
get
{
return this._AdminUID;
}
}
public string AdminPWD
{
set
{
this._AdminPWD = value;
}
get
{
return this._AdminPWD;
}
}
#endregion
public LoginModel()
{ }
}
}
接着是DAL的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
/*****************数据层********************/
namespace DAL
{
public class DAL
{
public DAL()
{
}
private string connstring =System.Configuration.ConfigurationManager.AppSettings["connstr"];//链接字符串
private SqlConnection conn = null;
private SqlConnection GetConnection()
{
if (conn == null)
{
conn = new SqlConnection(connstring);
}
return conn;
}
/// <summary>
/// 登录验证
/// </summary>
/// <param name="user">传入实体对象</param>
/// <returns>返回一个布尔值</returns>
public bool Exits(Model.LoginModel user)
{
using(GetConnection())
{
try
{
string sqlStr = "select count(1) from admin where adminuid='" + user.AdminUID.ToString() + "' and adminpwd='" + user.AdminPWD.ToString() + "'";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.Connection.Open();
int dataReater = int.Parse(cmd.ExecuteScalar().ToString());
if (dataReater==1)
{
return true;
}
else if (dataReater == 0)
{
return false;
}
else
{
return false;
}
cmd.Connection.Close();
}
catch (SqlException ex)
{
throw ex;
conn.Close();
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
}
}
BLL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using System.Data;
using System.Data.SqlClient;
/************业务逻辑层*****************/
namespace BLL
{
public class BLL
{
public BLL()
{
}
/// <summary>
/// 登录方法
/// </summary>
/// <param name="M"></param>
/// <returns></returns>
///
public static bool Login(string LoginName, string LoginPassword)
{
Model.LoginModel users = new LoginModel();
users.AdminUID = LoginName;
users.AdminPWD = LoginPassword;
DAL.DAL dals=new DAL.DAL();
return dals.Exits(users);
}
}
}
UI aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>登录界面</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server" Height="13px" Width="359px" style="font-weight: bold; font-size: small">
<table style="width: 372px">
<tr>
<td style="width: 81px">
<asp:Label ID="Label1" runat="server" Text="用户名"></asp:Label></td>
<td style="width: 146px">
<asp:TextBox ID="TextBox1" runat="server" Height="18px"></asp:TextBox></td>
<td style="width: 116px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="用户名不能为空" ForeColor="DarkGray" Width="126px"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 81px; height: 28px;">
<asp:Label ID="Label2" runat="server" Text="密 码"></asp:Label></td>
<td style="width: 146px; height: 28px;">
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
<td style="width: 116px; height: 28px;">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="密码不能为空" ForeColor="DarkGray"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 81px">
</td>
<td style="width: 146px">
<asp:LinkButton ID="LinkButton1" runat="server" Font-Underline="False"
ForeColor="Black" 录</asp:LinkButton>
<input type="reset" value="重置" /></td>
<td style="width: 116px">
</td>
</tr>
</table>
</asp:Panel>
</form>
</body>
</html>
UI cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using Model;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
bool isExits = BLL.BLL.Login(this.TextBox1.Text, this.TextBox2.Text);
if (isExits==true)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('登录成功!');</script>");
}
else if (isExits == false)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('错误!');</script>");
}
}
}
该例子实现了一个简单的三层登录功能!
创建一个asp.net三层架构
一个简单的三层结构demo:
首先我们来创建一个数据库ThreeLayer
可以把下面的:sql语句复制到查询分析器直接执行
use master
go
if exists (select * from sysdatabases where name=ThreeLayer)
drop database ThreeLayer
go
create database ThreeLayer
on
(
name='ThreeLayer_data',
filename='D:\SQLSERVER2000\MSSQL\Data\ThreeLayer_data.mdf',--我SQL装在D盘,你们可以换。
size=1mb,
filegrowth=10%
)
log on
(
name='ThreeLayer_log',
filename='D:\SQLSERVER2000\MSSQL\Data\ThreeLayer_log.ldf',
size=1mb,
filegrowth=10%
)
接着创建一张表Admin
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Admin]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Admin]
GO
CREATE TABLE [dbo].[Admin] (
[AdminId] [int] IDENTITY (1, 1) NOT NULL ,
[AdminUID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[AdminPWD] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
现在已经完成了数据库以及表的创建,接下来我们该创建三层架构的asp.net网站了。
一、打开vs.net 新建一个网站。命名为WebUIView,当然这个名字可以自己命名。
很明显,这个就是UI层,没错,这是给用户看的UI层。
二、我们先一个新项目命名为Model,这个实体层。
三、同样的道理我们添加一个新项目命名为DAL,这个数据访问层。
四、我们再添加一个新项目命名为BLL,这个数业务逻辑层。
接着,我们来添加引用,首先UI层引用BLL和Model,DAL引用Model,BLL引用DAL和Model
这样就完成了三层的初步架构。
当然实现三层的顺序是 Model--》DAL--》BLL--》UI
现在我把各个层的代码贴出来,首先是Model的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/****************实体层**********************/
namespace Model
{
public class LoginModel
{
#region 私有字段
private int _AdminId;
private string _AdminUID;
private string _AdminPWD;
#endregion
#region 属性
public int AdminId
{
set { this._AdminId=value; }
get { return _AdminId; }
}
public string AdminUID
{
set
{
this._AdminUID = value;
}
get
{
return this._AdminUID;
}
}
public string AdminPWD
{
set
{
this._AdminPWD = value;
}
get
{
return this._AdminPWD;
}
}
#endregion
public LoginModel()
{ }
}
}
接着是DAL的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
/*****************数据层********************/
namespace DAL
{
public class DAL
{
public DAL()
{
}
private string connstring =System.Configuration.ConfigurationManager.AppSettings["connstr"];//链接字符串
private SqlConnection conn = null;
private SqlConnection GetConnection()
{
if (conn == null)
{
conn = new SqlConnection(connstring);
}
return conn;
}
/// <summary>
/// 登录验证
/// </summary>
/// <param name="user">传入实体对象</param>
/// <returns>返回一个布尔值</returns>
public bool Exits(Model.LoginModel user)
{
using(GetConnection())
{
try
{
string sqlStr = "select count(1) from admin where adminuid='" + user.AdminUID.ToString() + "' and adminpwd='" + user.AdminPWD.ToString() + "'";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.Connection.Open();
int dataReater = int.Parse(cmd.ExecuteScalar().ToString());
if (dataReater==1)
{
return true;
}
else if (dataReater == 0)
{
return false;
}
else
{
return false;
}
cmd.Connection.Close();
}
catch (SqlException ex)
{
throw ex;
conn.Close();
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
}
}
}
BLL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using System.Data;
using System.Data.SqlClient;
/************业务逻辑层*****************/
namespace BLL
{
public class BLL
{
public BLL()
{
}
/// <summary>
/// 登录方法
/// </summary>
/// <param name="M"></param>
/// <returns></returns>
///
public static bool Login(string LoginName, string LoginPassword)
{
Model.LoginModel users = new LoginModel();
users.AdminUID = LoginName;
users.AdminPWD = LoginPassword;
DAL.DAL dals=new DAL.DAL();
return dals.Exits(users);
}
}
}
UI aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>登录界面</title>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server" Height="13px" Width="359px" style="font-weight: bold; font-size: small">
<table style="width: 372px">
<tr>
<td style="width: 81px">
<asp:Label ID="Label1" runat="server" Text="用户名"></asp:Label></td>
<td style="width: 146px">
<asp:TextBox ID="TextBox1" runat="server" Height="18px"></asp:TextBox></td>
<td style="width: 116px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="用户名不能为空" ForeColor="DarkGray" Width="126px"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 81px; height: 28px;">
<asp:Label ID="Label2" runat="server" Text="密 码"></asp:Label></td>
<td style="width: 146px; height: 28px;">
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
<td style="width: 116px; height: 28px;">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="密码不能为空" ForeColor="DarkGray"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 81px">
</td>
<td style="width: 146px">
<asp:LinkButton ID="LinkButton1" runat="server" Font-Underline="False"
ForeColor="Black" 录</asp:LinkButton>
<input type="reset" value="重置" /></td>
<td style="width: 116px">
</td>
</tr>
</table>
</asp:Panel>
</form>
</body>
</html>
UI cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using Model;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
bool isExits = BLL.BLL.Login(this.TextBox1.Text, this.TextBox2.Text);
if (isExits==true)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('登录成功!');</script>");
}
else if (isExits == false)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('错误!');</script>");
}
}
}
该例子实现了一个简单的三层登录功能!
相关文章推荐
- 创建一个可以绑定数据的 ASP.NET Web 部件控件
- ASP.NET三层架构的一个简单实现
- ASP.NET MVC+LINQ开发一个图书销售站点(6):创建数据层
- Scott Mitchell 的ASP.NET 2.0数据教程之二:创建一个业务逻辑层
- Scott Mitchell 的ASP.NET 2.0数据教程之一: 创建一个数据访问层
- asp.net 在TextBox里面创建一个默认提示,例如:搜索...。
- 通过ASP.NET Web API + JQuery创建一个简单的Web应用
- 创建一个基于ASP.NET 2.0的JSON WebService
- Asp.net中创建程序集以及一个简单的IHttpHandler 实现
- ASP.NET MVC+LINQ开发一个图书销售站点(5):创建逻辑层接口
- scott Mitchell 的ASP.NET 2.0数据教程之一: 创建一个数据访问层
- 如何创建一个新的ASP.NET项目
- [翻译]15分钟内创建一个电影的ASP.NET MVC数据库应用程序
- 【EntityFramework系列教程一,翻译】为ASP.NET MVC创建一个数据模型
- Asp.Net MVC(创建一个任务列表应用程序) - Part.1
- 在ASP.NET 2.0中操作数据之二:创建一个业务逻辑层
- Scott Mitchell 的ASP.NET 2.0数据教程之一: 创建一个数据访问层
- 使用Bootstrap和Web API创建一个ASP.NET web Forms 应用程序
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 在ASP.NET 2.0中操作数据:创建一个业务逻辑层