您的位置:首页 > 编程语言 > ASP

创建一个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>");

}

}

}

该例子实现了一个简单的三层登录功能!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐