一個簡單的三層結構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;
[NextPage]
}
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>");
}
}
}
該例子實作了一個簡單的三層登入功能!
轉載于:https://www.cnblogs.com/gaoxuzhao/archive/2011/10/14/2211151.html